Skip to content

Instantly share code, notes, and snippets.

@kentokento
Last active December 16, 2020 10:48
Show Gist options
  • Save kentokento/a77a490656599d1bb8e4ddb5f8b2d19e to your computer and use it in GitHub Desktop.
Save kentokento/a77a490656599d1bb8e4ddb5f8b2d19e to your computer and use it in GitHub Desktop.
func create(msg string) *appError {
var e appError
e.message = msg
e.frame = xerrors.Caller(2)
return &e
}
func New(msg string) AppError {
return create(msg)
}
func Errorf(format string, args ...interface{}) AppError {
return create(fmt.Sprintf(format, args...))
}
func Wrap(err error, msg ...string) AppError {
if err == nil {
return nil
}
var m string
if len(msg) != 0 {
m = msg[0]
}
e := create(m)
e.next = err
return e
}
func Wrapf(err error, format string, args ...interface{}) AppError {
e := create(fmt.Sprintf(format, args...))
e.next = err
return e
}
func As(err error, target interface{}) bool {
return xerrors.As(err, target)
}
func AsAppError(err error) *appError {
if err == nil {
return nil
}
var e *appError
if xerrors.As(err, &e) {
return e
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment