Categorygithub.com/pingcap/errors
modulepackage
0.11.4
Repository: https://github.com/pingcap/errors.git
Documentation: pkg.go.dev

# README

errors Travis-CI AppVeyor GoDoc Report card Sourcegraph

Package errors provides simple error handling primitives.

go get github.com/pkg/errors

The traditional error handling idiom in Go is roughly akin to

if err != nil {
        return err
}

which applied recursively up the call stack results in error reports without context or debugging information. The errors package allows programmers to add context to the failure path in their code in a way that does not destroy the original value of the error.

Adding context to an error

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

Retrieving the cause of an error

Using errors.Wrap constructs a stack of errors, adding context to the preceding error. Depending on the nature of the error it may be necessary to reverse the operation of errors.Wrap to retrieve the original error for inspection. Any error value which implements this interface can be inspected by errors.Cause.

type causer interface {
        Cause() error
}

errors.Cause will recursively retrieve 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
}

Read the package documentation for more information.

Contributing

We welcome pull requests, bug fixes and issue reports. With that said, the bar for adding new symbols to this package is intentionally set high.

Before proposing a change, please discuss your change by raising an issue.

License

BSD-2-Clause

# Functions

AddStack is similar to WithStack.
AlreadyExistsf represents an error with already exists message.
Annotate adds a message and ensures there is a stack trace.
Annotatef adds a message and ensures there is a stack trace.
BadRequestf represents an error with bad request message.
Cause returns the underlying cause of the error, if possible.
Errorf formats according to a format specifier and returns the string as a value that satisfies error.
Errors uses the ErrorGroup interface to return a slice of errors.
ErrorStack will format a stack trace if it is available, otherwise it will be Error() If the error is nil, the empty string is returned Note that this just calls fmt.Sprintf("%+v", err).
Find an error in the chain that matches a test function.
GetStackTracer will return the first StackTracer in the causer chain.
HasStack tells whether a StackTracer exists in the error chain.
IsAlreadyExists reports whether err was already exists error.
IsNotFound reports whether err was not found error.
New returns an error with the supplied message.
NewNoStackError creates error without error stack later duplicate trace will no longer generate Stack too.
NewStack is for library implementers that want to generate a stack trace.
NotFoundf represents an error with not found message.
NotSupportedf represents an error with not supported message.
NotValidf represents an error with not valid message.
SuspendStack suspends stack generate for error.
Trace just calls AddStack.
Unwrap uses causer to return the next error in the chain or nil.
WalkDeep does a depth-first traversal of all errors.
WithMessage annotates err with a new message.
WithStack annotates err with a stack trace at the point WithStack was called.
Wrap returns an error annotating err with a stack trace at the point Wrap is called, and the supplied message.
Wrapf returns an error annotating err with a stack trace at the point Wrapf is call, and the format specifier.

# Interfaces

ErrorGroup is an interface for multiple errors that are not a chain.
StackTraceAware is an optimization to avoid repetitive traversals of an error chain.
StackTracer retrieves the StackTrace Generally you would want to use the GetStackTracer function to do that.

# Type aliases

Frame represents a program counter inside a stack frame.
StackTrace is stack of Frames from innermost (newest) to outermost (oldest).