-
-
Save patryk4815/ab34922402609ef200c32e7f4dcd7af8 to your computer and use it in GitHub Desktop.
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
package main | |
func goRecover(f func(), recovered chan<- error) { | |
go func() { | |
done := false // just to handle panic(nil) https://github.com/golang/go/issues/25448 | |
defer func() { | |
if !done { | |
var errOut error | |
errAny := recover() | |
if err, ok := errAny.(error); ok { | |
// errors.Wrap adds stacktrace to error | |
errOut = errors.Wrap(err, "goRecover") | |
} else { | |
// errors.Errorf adds stacktrace to error | |
errOut = errors.Errorf("PANIC cast to error: %v", errAny) | |
} | |
recovered <- errOut | |
} | |
}() | |
f() | |
done = true | |
}() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment