Categorygithub.com/neighborly/go-errors
modulepackage
0.3.2
Repository: https://github.com/neighborly/go-errors.git
Documentation: pkg.go.dev

# README

go-errors

Error handling for Go. It uses pkg/errors under the hood.

Installation

go get github.com/nrfta/go-errors

Usage

New error

Create a new error. It uses pkg/errors for the actual error.

err := errors.New("an error message")

Wrapping errors

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")
}

Custom Error Code

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.

TypeString
InternalErrorInternal Error
NotFoundNot Found
InvalidArgumentInvalid Argument
UnauthenticatedUnauthenticated
PermissionDeniedPermission Denied
UnknownUnknown

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)

WithDisplayMessage

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!")

Retrieving display messages

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 Error
err := errors.NotFound.New("an error")
errors.DisplayMessage(err) // -> Not Found

Retrieving the cause of an error

Behaves 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
}

License

This project is licensed under the MIT License.

# Functions

Cause retrives the original error Note that it will return the error created internally from github.com/pkg/errors.
Code retrives the error code from an error, defaults to InternalError.
DisplayMessage retrives the display message.
New creates a no type error.
Newf creates an InternalError error with formatted message.
StackTrace retrives the stack trace of an error.
WithDisplayMessage returns a error containing a display message.
Wrap an error with a string.
Wrapf an error with format string.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Type aliases

ErrorCode holds the type of errors.