Skip to content

Instantly share code, notes, and snippets.

@kyoh86
Last active December 1, 2015 04:06
Show Gist options
  • Save kyoh86/423d182e8372170c8fd9 to your computer and use it in GitHub Desktop.
Save kyoh86/423d182e8372170c8fd9 to your computer and use it in GitHub Desktop.
recover and set state
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