Error handling for Go. It uses pkg/errors under the hood.
go get github.com/nrfta/go-errorsCreate a new error. It uses pkg/errors for the actual error.
err := errors.New("an error message")The errors.Wrap function returns a new error that adds context to the original error. For example
_, err := ioutil.ReadAll(r)
if err != nil {
return errors.Wrap(err, "read failed")
}We implement a custom error type/code for pre-defined errors. These are useful for application code to return a correct HTTP status or GraphQL error.
| Type | String |
|---|---|
InternalError |
Internal Error |
NotFound |
Not Found |
InvalidArgument |
Invalid Argument |
Unauthenticated |
Unauthenticated |
PermissionDenied |
Permission Denied |
Unknown |
Unknown |
You can create an error using the following:
err := errors.PermissionDenied.New("user does not have access")or you can wrap an existing as follows:
err = errors.PermissionDenied.Wrap(err)Use this function to a display message to an error. Usually, error messages are meant to be used internally only, instead of displaying them to users.
You can use errors.WithDisplayMessage to assign a display message to a given
error.
err := errors.New("internal message")
err = errors.WithDisplayMessage(err, "Display message goes here!")You can retrieve a display message by using errors.DisplayMessage.
If no message were assigned, it would use the error code string.
err := errors.New("internal message")
err = errors.WithDisplayMessage(err, "Display message goes here!")
errors.DisplayMessage(err) // -> Display message goes here!err := errors.New("an error")
errors.DisplayMessage(err) // -> Internal Errorerr := errors.NotFound.New("an error")
errors.DisplayMessage(err) // -> Not FoundBehaves the same as pkg/errors.
Note that if you create an error using this package, it returns the
underlying pkg/error error.
errors.Cause recursively retrieves the topmost error which does not
implement causer which is assumed to be the original cause. For example:
switch err := errors.Cause(err).(type) {
case *MyError
// handle specifically
default:
// unknown error
}This project is licensed under the MIT License.