Last active
June 29, 2019 08:22
-
-
Save markbates/12083cbb1e58d56bfb037be8f6cec2cc to your computer and use it in GitHub Desktop.
Unwrap Go errors from the go 2 errors proposal as well as github.com/pkg/errors
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 foo | |
// go2 errors | |
type wrapper interface { | |
Unwrap() error | |
} | |
// pkg/errors | |
type causer interface { | |
Cause() error | |
} | |
func unwrap(err error) error { | |
switch e := err.(type) { | |
case wrapper: | |
return e.Unwrap() | |
case causer: | |
return e.Cause() | |
} | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Last return in
unwrap
can be underdefault
statement inside the switch :)