Last active
January 14, 2021 08:17
-
-
Save Equanox/115dbb0d8352ea509cdb80091cf4cd21 to your computer and use it in GitHub Desktop.
Inspect Errors when using https://github.com/Benchkram/errz
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
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