Skip to content

Instantly share code, notes, and snippets.

@Equanox
Last active January 14, 2021 08:17
Show Gist options
  • Save Equanox/115dbb0d8352ea509cdb80091cf4cd21 to your computer and use it in GitHub Desktop.
Save Equanox/115dbb0d8352ea509cdb80091cf4cd21 to your computer and use it in GitHub Desktop.
Inspect Errors when using https://github.com/Benchkram/errz
var ErrDoOne = fmt.Errorf("Error Do One")
var ErrDoTwo = fmt.Errorf("Error Do Two")
func doOne() error {
return ErrDoOne
}
func doTwo() error {
return ErrDoTwo
}
// doMore returns ErrDoOne
func doMore() (err error) {
defer errz.Recover(&err) // Recover panics
// Do things
// ...
err := doOne()
errz.Fatal(err) // Panic + stack trace
err := doTwo()
errz.Fatal(err) // Panic + stack trace
return nil
}
// The highest layer
func doThings() {
err := doMore()
// Handle Error using https://golang.org/pkg/errors/
if err != nil {
// Using
if errors.Is(err, ErrDoOne) {
// Handle error One
} else if errors.Is(err, ErrDoTwo) {
// Handle error Two
} else {
// One of
errz.Log(err) // Print error + stack trace
errz.Fatal(err) // Panic, but also prints stack trace
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment