errors

package module
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2026 License: MIT Imports: 3 Imported by: 8

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.

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)
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.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Is     = errors.Is
	As     = errors.As
	Unwrap = errors.Unwrap
	Join   = errors.Join
)

Functions

func Cause

func Cause(err error) error

Cause retrives the original error Note that it will return the error created internally from github.com/pkg/errors

func DisplayMessage

func DisplayMessage(err error) string

DisplayMessage retrives the display message

func New

func New(msg string) error

New creates a no type error

func Newf

func Newf(msg string, args ...any) error

Newf creates an InternalError error with formatted message

func StackTrace

func StackTrace(err error) errors.StackTrace

StackTrace retrives the stack trace of an error

func WithDisplayMessage

func WithDisplayMessage(err error, msg string) error

WithDisplayMessage returns a error containing a display message

func Wrap

func Wrap(err error, msg string) error

Wrap an error with a string

func Wrapf

func Wrapf(err error, msg string, args ...any) error

Wrapf an error with format string

Types

type ErrorCode

type ErrorCode string

ErrorCode holds the type of errors

const (
	InternalError        ErrorCode = "Internal Error"
	NotFound             ErrorCode = "Not Found"
	InvalidArgument      ErrorCode = "Invalid Argument"
	Unauthenticated      ErrorCode = "Unauthenticated"
	PermissionDenied     ErrorCode = "Permission Denied"
	UnprocessableContent ErrorCode = "Unprocessable Content"
	Unknown              ErrorCode = "Unknown"
)

func Code

func Code(err error) ErrorCode

Code retrives the error code from an error, defaults to InternalError

func (ErrorCode) New

func (code ErrorCode) New(msg string) error

New creates a new customError

func (ErrorCode) Newf

func (code ErrorCode) Newf(msg string, args ...any) error

Newf creates a new customError with formatted message

func (ErrorCode) Wrap

func (code ErrorCode) Wrap(err error, msg string) error

Wrap creates a new wrapped error

func (ErrorCode) Wrapf

func (code ErrorCode) Wrapf(err error, msg string, args ...any) error

Wrapf creates a new wrapped error with formatted message

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL