Last active
September 8, 2019 23:01
-
-
Save smyrman/6856be04c6ba636a7d285857753ae7ad to your computer and use it in GitHub Desktop.
Go 1.13 const error example
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
pacakge x | |
const ( | |
ErrPermissoinDenied = constErr("permissoin denied") | |
ErrInvalidParameters = constErr("invalid parameters") | |
) | |
type constErr string | |
func (err constErr) Is(target error) bool { | |
s := target.Error() | |
if s == string(err) || strings.HasPrefix(s, string(err)+": ") | |
} | |
func (err constErr) wrap(err error) error { | |
return wrapErr{msg: string(err), err: err} | |
} | |
type wrapErr struct { | |
msg string | |
err error | |
} | |
func (err wrapErr) Is(target error) bool { | |
return constErr(err.msg).Is(target) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment