Last active
December 1, 2015 04:06
-
-
Save kyoh86/423d182e8372170c8fd9 to your computer and use it in GitHub Desktop.
recover and set state
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func do(ctx context.Context) (res *Result) { | |
defer func() { | |
if r := recover(); r != nil { | |
trace := make([]byte, 1024*10) | |
count := runtime.Stack(trace, true) | |
switch tr := r.(type) { | |
case error: | |
res = &Result{ | |
Err: tr, | |
} | |
default: | |
res = &Result{ | |
Err: fmt.Errorf("%v", tr), | |
} | |
} | |
logrus.Warnf("panic %s", res.Err.Error()) | |
logrus.Warnf("Stack of %d bytes: %s\n", count, string(trace[:count])) | |
} | |
}() | |
res = doCore(ctx) | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment