Documentation
¶
Overview ¶
Package errors provides simple error handling primitives and behavioral errors.
Hello there! Read the presentation http://dave.cheney.net/paste/gocon-spring-2016.pdf to see what the big deal is.
http://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully
Read this for asserting errors for their behaviour http://dave.cheney.net/2014/12/24/inspecting-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 by recording a stack trace at the point Wrap is called, and the supplied message. For example
_, err := ioutil.ReadAll(r)
if err != nil {
return errors.Wrap(err, "read failed")
}
If additional control is required the errors.WithStack and errors.WithMessage functions destructure errors.Wrap into its component operations of annotating an error with a stack trace and an a message, respectively.
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
type causer interface {
Cause() error
}
can be inspected by errors.Cause. 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
}
causer interface is not exported by this package, but is considered a part of stable public API.
Formatted printing of errors ¶
All error values returned from this package implement fmt.Formatter and can be formatted by the fmt package. The following verbs are supported
%s print the error. If the error has a Cause it will be
printed recursively
%v see %s
%+v extended format. Each Frame of the error's StackTrace will
be printed in detail.
Retrieving the stack trace of an error or wrapper ¶
New, Errorf, Wrap, and Wrapf record a stack trace at the point they are invoked. This information can be retrieved with the following interface.
type stackTracer interface {
StackTrace() errors.StackTrace
}
Where errors.StackTrace is defined as
type StackTrace []Frame
The Frame type represents a call site in the stack trace. Frame supports the fmt.Formatter interface that can be used for printing information about the stack trace of this error. For example:
if err, ok := err.(stackTracer); ok {
for _, f := range err.StackTrace() {
fmt.Printf("%+s:%d", f)
}
}
stackTracer interface is not exported by this package, but is considered a part of stable public API.
See the documentation for Frame.Format for more details.
Example (StackTrace) ¶
package main
import (
"fmt"
"github.com/pkg/errors"
)
func fn() error {
e1 := errors.New("error")
e2 := errors.Wrap(e1, "inner")
e3 := errors.Wrap(e2, "middle")
return errors.Wrap(e3, "outer")
}
func main() {
type stackTracer interface {
StackTrace() errors.StackTrace
}
err, ok := errors.Cause(fn()).(stackTracer)
if !ok {
panic("oops, err does not implement stackTracer")
}
st := err.StackTrace()
fmt.Printf("%+v", st[0:2]) // top two frames
// Example output:
// github.com/pkg/errors_test.fn
// /home/dfc/src/github.com/pkg/errors/example_test.go:47
// github.com/pkg/errors_test.Example_stackTrace
// /home/dfc/src/github.com/pkg/errors/example_test.go:127
}
Index ¶
- func Cause(err error) error
- func CausedBehaviour(err error, bhf BehaviourFunc) bool
- func Errorf(format string, args ...interface{}) error
- func FormatLineFunc(errs []error) string
- func HasBehaviour(err error, bfs ...BehaviourFunc) bool
- func IsAborted(err error) bool
- func IsAlreadyCaptured(err error) bool
- func IsAlreadyClosed(err error) bool
- func IsAlreadyExists(err error) bool
- func IsAlreadyInUse(err error) bool
- func IsAlreadyRefunded(err error) bool
- func IsBlocked(err error) bool
- func IsConnectionFailed(err error) bool
- func IsDeclined(err error) bool
- func IsDenied(err error) bool
- func IsDuplicated(err error) bool
- func IsEmpty(err error) bool
- func IsExceeded(err error) bool
- func IsExpired(err error) bool
- func IsFatal(err error) bool
- func IsInProgress(err error) bool
- func IsInsufficient(err error) bool
- func IsInterrupted(err error) bool
- func IsLocked(err error) bool
- func IsMismatch(err error) bool
- func IsNotAcceptable(err error) bool
- func IsNotAllowed(err error) bool
- func IsNotFound(err error) bool
- func IsNotImplemented(err error) bool
- func IsNotRecoverable(err error) bool
- func IsNotSupported(err error) bool
- func IsNotValid(err error) bool
- func IsOverflowed(err error) bool
- func IsPermissionDenied(err error) bool
- func IsQuotaExceeded(err error) bool
- func IsReadFailed(err error) bool
- func IsRejected(err error) bool
- func IsRequired(err error) bool
- func IsRestricted(err error) bool
- func IsRevoked(err error) bool
- func IsTemporary(err error) bool
- func IsTerminated(err error) bool
- func IsTimeout(err error) bool
- func IsTooLarge(err error) bool
- func IsUnauthorized(err error) bool
- func IsUnavailable(err error) bool
- func IsUserNotFound(err error) bool
- func IsVerificationFailed(err error) bool
- func IsWriteFailed(err error) bool
- func IsWrongVersion(err error) bool
- func MultiErrContainsAll(err error, bfs ...BehaviourFunc) bool
- func MultiErrContainsAny(err error, bfs ...BehaviourFunc) bool
- func New(message string) error
- func NewAborted(err error, msg string, args ...interface{}) error
- func NewAbortedf(format string, args ...interface{}) error
- func NewAlreadyCaptured(err error, msg string, args ...interface{}) error
- func NewAlreadyCapturedf(format string, args ...interface{}) error
- func NewAlreadyClosed(err error, msg string, args ...interface{}) error
- func NewAlreadyClosedf(format string, args ...interface{}) error
- func NewAlreadyExists(err error, msg string, args ...interface{}) error
- func NewAlreadyExistsf(format string, args ...interface{}) error
- func NewAlreadyInUse(err error, msg string, args ...interface{}) error
- func NewAlreadyInUsef(format string, args ...interface{}) error
- func NewAlreadyRefunded(err error, msg string, args ...interface{}) error
- func NewAlreadyRefundedf(format string, args ...interface{}) error
- func NewBlocked(err error, msg string, args ...interface{}) error
- func NewBlockedf(format string, args ...interface{}) error
- func NewConnectionFailed(err error, msg string, args ...interface{}) error
- func NewConnectionFailedf(format string, args ...interface{}) error
- func NewDeclined(err error, msg string, args ...interface{}) error
- func NewDeclinedf(format string, args ...interface{}) error
- func NewDenied(err error, msg string, args ...interface{}) error
- func NewDeniedf(format string, args ...interface{}) error
- func NewDuplicated(err error, msg string, args ...interface{}) error
- func NewDuplicatedf(format string, args ...interface{}) error
- func NewEmpty(err error, msg string, args ...interface{}) error
- func NewEmptyf(format string, args ...interface{}) error
- func NewExceeded(err error, msg string, args ...interface{}) error
- func NewExceededf(format string, args ...interface{}) error
- func NewExpired(err error, msg string, args ...interface{}) error
- func NewExpiredf(format string, args ...interface{}) error
- func NewFatal(err error, msg string, args ...interface{}) error
- func NewFatalf(format string, args ...interface{}) error
- func NewInProgress(err error, msg string, args ...interface{}) error
- func NewInProgressf(format string, args ...interface{}) error
- func NewInsufficient(err error, msg string, args ...interface{}) error
- func NewInsufficientf(format string, args ...interface{}) error
- func NewInterrupted(err error, msg string, args ...interface{}) error
- func NewInterruptedf(format string, args ...interface{}) error
- func NewLocked(err error, msg string, args ...interface{}) error
- func NewLockedf(format string, args ...interface{}) error
- func NewMismatch(err error, msg string, args ...interface{}) error
- func NewMismatchf(format string, args ...interface{}) error
- func NewNotAcceptable(err error, msg string, args ...interface{}) error
- func NewNotAcceptablef(format string, args ...interface{}) error
- func NewNotAllowed(err error, msg string, args ...interface{}) error
- func NewNotAllowedf(format string, args ...interface{}) error
- func NewNotFound(err error, msg string, args ...interface{}) error
- func NewNotFoundf(format string, args ...interface{}) error
- func NewNotImplemented(err error, msg string, args ...interface{}) error
- func NewNotImplementedf(format string, args ...interface{}) error
- func NewNotRecoverable(err error, msg string, args ...interface{}) error
- func NewNotRecoverablef(format string, args ...interface{}) error
- func NewNotSupported(err error, msg string, args ...interface{}) error
- func NewNotSupportedf(format string, args ...interface{}) error
- func NewNotValid(err error, msg string, args ...interface{}) error
- func NewNotValidf(format string, args ...interface{}) error
- func NewOverflowed(err error, msg string, args ...interface{}) error
- func NewOverflowedf(format string, args ...interface{}) error
- func NewPermissionDenied(err error, msg string, args ...interface{}) error
- func NewPermissionDeniedf(format string, args ...interface{}) error
- func NewQuotaExceeded(err error, msg string, args ...interface{}) error
- func NewQuotaExceededf(format string, args ...interface{}) error
- func NewReadFailed(err error, msg string, args ...interface{}) error
- func NewReadFailedf(format string, args ...interface{}) error
- func NewRejected(err error, msg string, args ...interface{}) error
- func NewRejectedf(format string, args ...interface{}) error
- func NewRequired(err error, msg string, args ...interface{}) error
- func NewRequiredf(format string, args ...interface{}) error
- func NewRestricted(err error, msg string, args ...interface{}) error
- func NewRestrictedf(format string, args ...interface{}) error
- func NewRevoked(err error, msg string, args ...interface{}) error
- func NewRevokedf(format string, args ...interface{}) error
- func NewTemporary(err error, msg string, args ...interface{}) error
- func NewTemporaryf(format string, args ...interface{}) error
- func NewTerminated(err error, msg string, args ...interface{}) error
- func NewTerminatedf(format string, args ...interface{}) error
- func NewTimeout(err error, msg string, args ...interface{}) error
- func NewTimeoutf(format string, args ...interface{}) error
- func NewTooLarge(err error, msg string, args ...interface{}) error
- func NewTooLargef(format string, args ...interface{}) error
- func NewUnauthorized(err error, msg string, args ...interface{}) error
- func NewUnauthorizedf(format string, args ...interface{}) error
- func NewUnavailable(err error, msg string, args ...interface{}) error
- func NewUnavailablef(format string, args ...interface{}) error
- func NewUserNotFound(err error, msg string, args ...interface{}) error
- func NewUserNotFoundf(format string, args ...interface{}) error
- func NewVerificationFailed(err error, msg string, args ...interface{}) error
- func NewVerificationFailedf(format string, args ...interface{}) error
- func NewWriteFailed(err error, msg string, args ...interface{}) error
- func NewWriteFailedf(format string, args ...interface{}) error
- func NewWrongVersion(err error, msg string, args ...interface{}) error
- func NewWrongVersionf(format string, args ...interface{}) error
- func WithMessage(err error, message string) error
- func WithStack(err error) error
- func Wrap(err error, message string) error
- func Wrapf(err error, format string, args ...interface{}) error
- type BehaviourFunc
- type Error
- type ErrorFormatFunc
- type Frame
- type MultiErr
- type StackTrace
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Cause ¶
Cause returns the underlying cause of the error, if possible. An error value has a cause if it implements the following interface:
type causer interface {
Cause() error
}
If the error does not implement Cause, the original error will be returned. If the error is nil, nil will be returned without further investigation.
Example ¶
package main
import (
"fmt"
"github.com/pkg/errors"
)
func fn() error {
e1 := errors.New("error")
e2 := errors.Wrap(e1, "inner")
e3 := errors.Wrap(e2, "middle")
return errors.Wrap(e3, "outer")
}
func main() {
err := fn()
fmt.Println(err)
fmt.Println(errors.Cause(err))
}
Output: outer: middle: inner: error error
Example (Printf) ¶
package main
import (
"fmt"
"github.com/pkg/errors"
)
func main() {
err := errors.Wrap(func() error {
return func() error {
return errors.Errorf("hello %s", fmt.Sprintf("world"))
}()
}(), "failed")
fmt.Printf("%v", err)
}
Output: failed: hello world
func CausedBehaviour ¶
func CausedBehaviour(err error, bhf BehaviourFunc) bool
CausedBehaviour returns the first underlying caused behaviour of the error, if possible. An error value has a cause if it implements the following interface:
type Causer interface {
Cause() error
}
If the error does not implement Cause or is nil, false will be returned. The variable bhf gets called on each unwrapped "cause" error
func Errorf ¶
Errorf formats according to a format specifier and returns the string as a value that satisfies error. Errorf also records the stack trace at the point it was called.
Example (Extended) ¶
package main
import (
"fmt"
"github.com/pkg/errors"
)
func main() {
err := errors.Errorf("whoops: %s", "foo")
fmt.Printf("%+v", err)
// Example output:
// whoops: foo
// github.com/pkg/errors_test.ExampleErrorf
// /home/dfc/src/github.com/pkg/errors/example_test.go:101
// testing.runExample
// /home/dfc/go/src/testing/example.go:114
// testing.RunExamples
// /home/dfc/go/src/testing/example.go:38
// testing.(*M).Run
// /home/dfc/go/src/testing/testing.go:744
// main.main
// /github.com/pkg/errors/_test/_testmain.go:102
// runtime.main
// /home/dfc/go/src/runtime/proc.go:183
// runtime.goexit
// /home/dfc/go/src/runtime/asm_amd64.s:2059
}
func FormatLineFunc ¶
FormatLineFunc is a basic formatter that outputs the errors that occurred along with the filename and the line number of the errors. Only if the error has a Location() function.
func HasBehaviour ¶
func HasBehaviour(err error, bfs ...BehaviourFunc) bool
HasBehaviour checks if err contains at least one of the provided behaviour functions. Does not traverse recursive into the error.
func IsAborted ¶
IsAborted reports whether err was created with NewAborted() or implements interface:
type Aborteder interface {
Aborted() bool
}
func IsAlreadyCaptured ¶
IsAlreadyCaptured reports whether err was created with NewAlreadyCaptured() or implements interface:
type AlreadyCaptureder interface {
AlreadyCaptured() bool
}
func IsAlreadyClosed ¶
IsAlreadyClosed reports whether err was created with NewAlreadyClosed() or implements interface:
type AlreadyCloseder interface {
AlreadyClosed() bool
}
func IsAlreadyExists ¶
IsAlreadyExists reports whether err was created with NewAlreadyExists() or implements interface:
type AlreadyExistser interface {
AlreadyExists() bool
}
func IsAlreadyInUse ¶
IsAlreadyInUse reports whether err was created with NewAlreadyInUse() or implements interface:
type AlreadyInUseer interface {
AlreadyInUse() bool
}
func IsAlreadyRefunded ¶
IsAlreadyRefunded reports whether err was created with NewAlreadyRefunded() or implements interface:
type AlreadyRefundeder interface {
AlreadyRefunded() bool
}
func IsBlocked ¶
IsBlocked reports whether err was created with NewBlocked() or implements interface:
type Blockeder interface {
Blocked() bool
}
func IsConnectionFailed ¶
IsConnectionFailed reports whether err was created with NewConnectionFailed() or implements interface:
type ConnectionFaileder interface {
ConnectionFailed() bool
}
func IsDeclined ¶
IsDeclined reports whether err was created with NewDeclined() or implements interface:
type Declineder interface {
Declined() bool
}
func IsDenied ¶
IsDenied reports whether err was created with NewDenied() or implements interface:
type Denieder interface {
Denied() bool
}
func IsDuplicated ¶
IsDuplicated reports whether err was created with NewDuplicated() or implements interface:
type Duplicateder interface {
Duplicated() bool
}
func IsEmpty ¶
IsEmpty reports whether err was created with NewEmpty() or implements interface:
type Emptyer interface {
Empty() bool
}
func IsExceeded ¶
IsExceeded reports whether err was created with NewExceeded() or implements interface:
type Exceededer interface {
Exceeded() bool
}
func IsExpired ¶
IsExpired reports whether err was created with NewExpired() or implements interface:
type Expireder interface {
Expired() bool
}
func IsFatal ¶
IsFatal reports whether err was created with NewFatal() or implements interface:
type Fataler interface {
Fatal() bool
}
func IsInProgress ¶
IsInProgress reports whether err was created with NewInProgress() or implements interface:
type InProgresser interface {
InProgress() bool
}
func IsInsufficient ¶
IsInsufficient reports whether err was created with NewInsufficient() or implements interface:
type Insufficienter interface {
Insufficient() bool
}
func IsInterrupted ¶
IsInterrupted reports whether err was created with NewInterrupted() or implements interface:
type Interrupteder interface {
Interrupted() bool
}
func IsLocked ¶
IsLocked reports whether err was created with NewLocked() or implements interface:
type Lockeder interface {
Locked() bool
}
func IsMismatch ¶
IsMismatch reports whether err was created with NewMismatch() or implements interface:
type Mismatcher interface {
Mismatch() bool
}
func IsNotAcceptable ¶
IsNotAcceptable reports whether err was created with NewNotAcceptable() or implements interface:
type NotAcceptableer interface {
NotAcceptable() bool
}
func IsNotAllowed ¶
IsNotAllowed reports whether err was created with NewNotAllowed() or implements interface:
type NotAlloweder interface {
NotAllowed() bool
}
func IsNotFound ¶
IsNotFound reports whether err was created with NewNotFound() or implements interface:
type NotFounder interface {
NotFound() bool
}
func IsNotImplemented ¶
IsNotImplemented reports whether err was created with NewNotImplemented() or implements interface:
type NotImplementeder interface {
NotImplemented() bool
}
func IsNotRecoverable ¶
IsNotRecoverable reports whether err was created with NewNotRecoverable() or implements interface:
type NotRecoverableer interface {
NotRecoverable() bool
}
func IsNotSupported ¶
IsNotSupported reports whether err was created with NewNotSupported() or implements interface:
type NotSupporteder interface {
NotSupported() bool
}
func IsNotValid ¶
IsNotValid reports whether err was created with NewNotValid() or implements interface:
type NotValider interface {
NotValid() bool
}
func IsOverflowed ¶
IsOverflowed reports whether err was created with NewOverflowed() or implements interface:
type Overfloweder interface {
Overflowed() bool
}
func IsPermissionDenied ¶
IsPermissionDenied reports whether err was created with NewPermissionDenied() or implements interface:
type PermissionDenieder interface {
PermissionDenied() bool
}
func IsQuotaExceeded ¶
IsQuotaExceeded reports whether err was created with NewQuotaExceeded() or implements interface:
type QuotaExceededer interface {
QuotaExceeded() bool
}
func IsReadFailed ¶
IsReadFailed reports whether err was created with NewReadFailed() or implements interface:
type ReadFaileder interface {
ReadFailed() bool
}
func IsRejected ¶
IsRejected reports whether err was created with NewRejected() or implements interface:
type Rejecteder interface {
Rejected() bool
}
func IsRequired ¶
IsRequired reports whether err was created with NewRequired() or implements interface:
type Requireder interface {
Required() bool
}
func IsRestricted ¶
IsRestricted reports whether err was created with NewRestricted() or implements interface:
type Restricteder interface {
Restricted() bool
}
func IsRevoked ¶
IsRevoked reports whether err was created with NewRevoked() or implements interface:
type Revokeder interface {
Revoked() bool
}
func IsTemporary ¶
IsTemporary reports whether err was created with NewTemporary() or implements interface:
type Temporaryer interface {
Temporary() bool
}
func IsTerminated ¶
IsTerminated reports whether err was created with NewTerminated() or implements interface:
type Terminateder interface {
Terminated() bool
}
func IsTimeout ¶
IsTimeout reports whether err was created with NewTimeout() or implements interface:
type Timeouter interface {
Timeout() bool
}
func IsTooLarge ¶
IsTooLarge reports whether err was created with NewTooLarge() or implements interface:
type TooLargeer interface {
TooLarge() bool
}
func IsUnauthorized ¶
IsUnauthorized reports whether err was created with NewUnauthorized() or implements interface:
type Unauthorizeder interface {
Unauthorized() bool
}
func IsUnavailable ¶
IsUnavailable reports whether err was created with NewUnavailable() or implements interface:
type Unavailableer interface {
Unavailable() bool
}
func IsUserNotFound ¶
IsUserNotFound reports whether err was created with NewUserNotFound() or implements interface:
type UserNotFounder interface {
UserNotFound() bool
}
func IsVerificationFailed ¶
IsVerificationFailed reports whether err was created with NewVerificationFailed() or implements interface:
type VerificationFaileder interface {
VerificationFailed() bool
}
func IsWriteFailed ¶
IsWriteFailed reports whether err was created with NewWriteFailed() or implements interface:
type WriteFaileder interface {
WriteFailed() bool
}
func IsWrongVersion ¶
IsWrongVersion reports whether err was created with NewWrongVersion() or implements interface:
type WrongVersioner interface {
WrongVersion() bool
}
func MultiErrContainsAll ¶
func MultiErrContainsAll(err error, bfs ...BehaviourFunc) bool
MultiErrContainsAll checks if err contains a behavioral error. 1st argument err must be of type (*MultiErr) and validate function vf at least one of the many Is*() e.g. IsNotValid(), see type BehaviourFunc. All validate functions must return true. If there are multiple behavioral errors and one BehaviourFunc it will stop after all errors matches the BehaviourFunc, not at the first match.
func MultiErrContainsAny ¶
func MultiErrContainsAny(err error, bfs ...BehaviourFunc) bool
MultiErrContainsAny checks if err contains at least one behavioral error. 1st argument err must be of type (*MultiErr) and validate function vf at least one of the many Is*() e.g. IsNotValid(), see type BehaviourFunc.
func New ¶
New returns an error with the supplied message. New also records the stack trace at the point it was called.
Example ¶
package main
import (
"fmt"
"github.com/pkg/errors"
)
func main() {
err := errors.New("whoops")
fmt.Println(err)
}
Output: whoops
Example (Printf) ¶
package main
import (
"fmt"
"github.com/pkg/errors"
)
func main() {
err := errors.New("whoops")
fmt.Printf("%+v", err)
// Example output:
// whoops
// github.com/pkg/errors_test.ExampleNew_printf
// /home/dfc/src/github.com/pkg/errors/example_test.go:17
// testing.runExample
// /home/dfc/go/src/testing/example.go:114
// testing.RunExamples
// /home/dfc/go/src/testing/example.go:38
// testing.(*M).Run
// /home/dfc/go/src/testing/testing.go:744
// main.main
// /github.com/pkg/errors/_test/_testmain.go:106
// runtime.main
// /home/dfc/go/src/runtime/proc.go:183
// runtime.goexit
// /home/dfc/go/src/runtime/asm_amd64.s:2059
}
func NewAborted ¶
NewAborted returns an error which wraps err that satisfies IsAborted().
func NewAbortedf ¶
NewAbortedf returns a formatted error that satisfies IsAborted().
func NewAlreadyCaptured ¶
NewAlreadyCaptured returns an error which wraps err that satisfies IsAlreadyCaptured().
func NewAlreadyCapturedf ¶
NewAlreadyCapturedf returns a formatted error that satisfies IsAlreadyCaptured().
func NewAlreadyClosed ¶
NewAlreadyClosed returns an error which wraps err that satisfies IsAlreadyClosed().
func NewAlreadyClosedf ¶
NewAlreadyClosedf returns a formatted error that satisfies IsAlreadyClosed().
func NewAlreadyExists ¶
NewAlreadyExists returns an error which wraps err that satisfies IsAlreadyExists().
func NewAlreadyExistsf ¶
NewAlreadyExistsf returns a formatted error that satisfies IsAlreadyExists().
func NewAlreadyInUse ¶
NewAlreadyInUse returns an error which wraps err that satisfies IsAlreadyInUse().
func NewAlreadyInUsef ¶
NewAlreadyInUsef returns a formatted error that satisfies IsAlreadyInUse().
func NewAlreadyRefunded ¶
NewAlreadyRefunded returns an error which wraps err that satisfies IsAlreadyRefunded().
func NewAlreadyRefundedf ¶
NewAlreadyRefundedf returns a formatted error that satisfies IsAlreadyRefunded().
func NewBlocked ¶
NewBlocked returns an error which wraps err that satisfies IsBlocked().
func NewBlockedf ¶
NewBlockedf returns a formatted error that satisfies IsBlocked().
func NewConnectionFailed ¶
NewConnectionFailed returns an error which wraps err that satisfies IsConnectionFailed().
func NewConnectionFailedf ¶
NewConnectionFailedf returns a formatted error that satisfies IsConnectionFailed().
func NewDeclined ¶
NewDeclined returns an error which wraps err that satisfies IsDeclined().
func NewDeclinedf ¶
NewDeclinedf returns a formatted error that satisfies IsDeclined().
func NewDeniedf ¶
NewDeniedf returns a formatted error that satisfies IsDenied().
func NewDuplicated ¶
NewDuplicated returns an error which wraps err that satisfies IsDuplicated().
func NewDuplicatedf ¶
NewDuplicatedf returns a formatted error that satisfies IsDuplicated().
func NewExceeded ¶
NewExceeded returns an error which wraps err that satisfies IsExceeded().
func NewExceededf ¶
NewExceededf returns a formatted error that satisfies IsExceeded().
func NewExpired ¶
NewExpired returns an error which wraps err that satisfies IsExpired().
func NewExpiredf ¶
NewExpiredf returns a formatted error that satisfies IsExpired().
func NewInProgress ¶
NewInProgress returns an error which wraps err that satisfies IsInProgress().
func NewInProgressf ¶
NewInProgressf returns a formatted error that satisfies IsInProgress().
func NewInsufficient ¶
NewInsufficient returns an error which wraps err that satisfies IsInsufficient().
func NewInsufficientf ¶
NewInsufficientf returns a formatted error that satisfies IsInsufficient().
func NewInterrupted ¶
NewInterrupted returns an error which wraps err that satisfies IsInterrupted().
func NewInterruptedf ¶
NewInterruptedf returns a formatted error that satisfies IsInterrupted().
func NewLockedf ¶
NewLockedf returns a formatted error that satisfies IsLocked().
func NewMismatch ¶
NewMismatch returns an error which wraps err that satisfies IsMismatch().
func NewMismatchf ¶
NewMismatchf returns a formatted error that satisfies IsMismatch().
func NewNotAcceptable ¶
NewNotAcceptable returns an error which wraps err that satisfies IsNotAcceptable().
func NewNotAcceptablef ¶
NewNotAcceptablef returns a formatted error that satisfies IsNotAcceptable().
func NewNotAllowed ¶
NewNotAllowed returns an error which wraps err that satisfies IsNotAllowed().
func NewNotAllowedf ¶
NewNotAllowedf returns a formatted error that satisfies IsNotAllowed().
func NewNotFound ¶
NewNotFound returns an error which wraps err that satisfies IsNotFound().
func NewNotFoundf ¶
NewNotFoundf returns a formatted error that satisfies IsNotFound().
func NewNotImplemented ¶
NewNotImplemented returns an error which wraps err that satisfies IsNotImplemented().
func NewNotImplementedf ¶
NewNotImplementedf returns a formatted error that satisfies IsNotImplemented().
func NewNotRecoverable ¶
NewNotRecoverable returns an error which wraps err that satisfies IsNotRecoverable().
func NewNotRecoverablef ¶
NewNotRecoverablef returns a formatted error that satisfies IsNotRecoverable().
func NewNotSupported ¶
NewNotSupported returns an error which wraps err that satisfies IsNotSupported().
func NewNotSupportedf ¶
NewNotSupportedf returns a formatted error that satisfies IsNotSupported().
func NewNotValid ¶
NewNotValid returns an error which wraps err that satisfies IsNotValid().
func NewNotValidf ¶
NewNotValidf returns a formatted error that satisfies IsNotValid().
func NewOverflowed ¶
NewOverflowed returns an error which wraps err that satisfies IsOverflowed().
func NewOverflowedf ¶
NewOverflowedf returns a formatted error that satisfies IsOverflowed().
func NewPermissionDenied ¶
NewPermissionDenied returns an error which wraps err that satisfies IsPermissionDenied().
func NewPermissionDeniedf ¶
NewPermissionDeniedf returns a formatted error that satisfies IsPermissionDenied().
func NewQuotaExceeded ¶
NewQuotaExceeded returns an error which wraps err that satisfies IsQuotaExceeded().
func NewQuotaExceededf ¶
NewQuotaExceededf returns a formatted error that satisfies IsQuotaExceeded().
func NewReadFailed ¶
NewReadFailed returns an error which wraps err that satisfies IsReadFailed().
func NewReadFailedf ¶
NewReadFailedf returns a formatted error that satisfies IsReadFailed().
func NewRejected ¶
NewRejected returns an error which wraps err that satisfies IsRejected().
func NewRejectedf ¶
NewRejectedf returns a formatted error that satisfies IsRejected().
func NewRequired ¶
NewRequired returns an error which wraps err that satisfies IsRequired().
func NewRequiredf ¶
NewRequiredf returns a formatted error that satisfies IsRequired().
func NewRestricted ¶
NewRestricted returns an error which wraps err that satisfies IsRestricted().
func NewRestrictedf ¶
NewRestrictedf returns a formatted error that satisfies IsRestricted().
func NewRevoked ¶
NewRevoked returns an error which wraps err that satisfies IsRevoked().
func NewRevokedf ¶
NewRevokedf returns a formatted error that satisfies IsRevoked().
func NewTemporary ¶
NewTemporary returns an error which wraps err that satisfies IsTemporary().
func NewTemporaryf ¶
NewTemporaryf returns a formatted error that satisfies IsTemporary().
func NewTerminated ¶
NewTerminated returns an error which wraps err that satisfies IsTerminated().
func NewTerminatedf ¶
NewTerminatedf returns a formatted error that satisfies IsTerminated().
func NewTimeout ¶
NewTimeout returns an error which wraps err that satisfies IsTimeout().
func NewTimeoutf ¶
NewTimeoutf returns a formatted error that satisfies IsTimeout().
func NewTooLarge ¶
NewTooLarge returns an error which wraps err that satisfies IsTooLarge().
func NewTooLargef ¶
NewTooLargef returns a formatted error that satisfies IsTooLarge().
func NewUnauthorized ¶
NewUnauthorized returns an error which wraps err that satisfies IsUnauthorized().
func NewUnauthorizedf ¶
NewUnauthorizedf returns a formatted error that satisfies IsUnauthorized().
func NewUnavailable ¶
NewUnavailable returns an error which wraps err that satisfies IsUnavailable().
func NewUnavailablef ¶
NewUnavailablef returns a formatted error that satisfies IsUnavailable().
func NewUserNotFound ¶
NewUserNotFound returns an error which wraps err that satisfies IsUserNotFound().
func NewUserNotFoundf ¶
NewUserNotFoundf returns a formatted error that satisfies IsUserNotFound().
func NewVerificationFailed ¶
NewVerificationFailed returns an error which wraps err that satisfies IsVerificationFailed().
func NewVerificationFailedf ¶
NewVerificationFailedf returns a formatted error that satisfies IsVerificationFailed().
func NewWriteFailed ¶
NewWriteFailed returns an error which wraps err that satisfies IsWriteFailed().
func NewWriteFailedf ¶
NewWriteFailedf returns a formatted error that satisfies IsWriteFailed().
func NewWrongVersion ¶
NewWrongVersion returns an error which wraps err that satisfies IsWrongVersion().
func NewWrongVersionf ¶
NewWrongVersionf returns a formatted error that satisfies IsWrongVersion().
func WithMessage ¶
WithMessage annotates err with a new message. If err is nil, WithMessage returns nil.
Example ¶
package main
import (
"fmt"
"github.com/pkg/errors"
)
func main() {
cause := errors.New("whoops")
err := errors.WithMessage(cause, "oh noes")
fmt.Println(err)
}
Output: oh noes: whoops
func WithStack ¶
WithStack annotates err with a stack trace at the point WithStack was called. If err is nil, WithStack returns nil.
Example ¶
package main
import (
"fmt"
"github.com/pkg/errors"
)
func main() {
cause := errors.New("whoops")
err := errors.WithStack(cause)
fmt.Println(err)
}
Output: whoops
Example (Printf) ¶
package main
import (
"fmt"
"github.com/pkg/errors"
)
func main() {
cause := errors.New("whoops")
err := errors.WithStack(cause)
fmt.Printf("%+v", err)
// Example Output:
// whoops
// github.com/pkg/errors_test.ExampleWithStack_printf
// /home/fabstu/go/src/github.com/pkg/errors/example_test.go:55
// testing.runExample
// /usr/lib/go/src/testing/example.go:114
// testing.RunExamples
// /usr/lib/go/src/testing/example.go:38
// testing.(*M).Run
// /usr/lib/go/src/testing/testing.go:744
// main.main
// github.com/pkg/errors/_test/_testmain.go:106
// runtime.main
// /usr/lib/go/src/runtime/proc.go:183
// runtime.goexit
// /usr/lib/go/src/runtime/asm_amd64.s:2086
// github.com/pkg/errors_test.ExampleWithStack_printf
// /home/fabstu/go/src/github.com/pkg/errors/example_test.go:56
// testing.runExample
// /usr/lib/go/src/testing/example.go:114
// testing.RunExamples
// /usr/lib/go/src/testing/example.go:38
// testing.(*M).Run
// /usr/lib/go/src/testing/testing.go:744
// main.main
// github.com/pkg/errors/_test/_testmain.go:106
// runtime.main
// /usr/lib/go/src/runtime/proc.go:183
// runtime.goexit
// /usr/lib/go/src/runtime/asm_amd64.s:2086
}
func Wrap ¶
Wrap returns an error annotating err with a stack trace at the point Wrap is called, and the supplied message. If err is nil, Wrap returns nil.
Example ¶
package main
import (
"fmt"
"github.com/pkg/errors"
)
func main() {
cause := errors.New("whoops")
err := errors.Wrap(cause, "oh noes")
fmt.Println(err)
}
Output: oh noes: whoops
Example (Extended) ¶
package main
import (
"fmt"
"github.com/pkg/errors"
)
func fn() error {
e1 := errors.New("error")
e2 := errors.Wrap(e1, "inner")
e3 := errors.Wrap(e2, "middle")
return errors.Wrap(e3, "outer")
}
func main() {
err := fn()
fmt.Printf("%+v\n", err)
// Example output:
// error
// github.com/pkg/errors_test.fn
// /home/dfc/src/github.com/pkg/errors/example_test.go:47
// github.com/pkg/errors_test.ExampleCause_printf
// /home/dfc/src/github.com/pkg/errors/example_test.go:63
// testing.runExample
// /home/dfc/go/src/testing/example.go:114
// testing.RunExamples
// /home/dfc/go/src/testing/example.go:38
// testing.(*M).Run
// /home/dfc/go/src/testing/testing.go:744
// main.main
// /github.com/pkg/errors/_test/_testmain.go:104
// runtime.main
// /home/dfc/go/src/runtime/proc.go:183
// runtime.goexit
// /home/dfc/go/src/runtime/asm_amd64.s:2059
// github.com/pkg/errors_test.fn
// /home/dfc/src/github.com/pkg/errors/example_test.go:48: inner
// github.com/pkg/errors_test.fn
// /home/dfc/src/github.com/pkg/errors/example_test.go:49: middle
// github.com/pkg/errors_test.fn
// /home/dfc/src/github.com/pkg/errors/example_test.go:50: outer
}
func Wrapf ¶
Wrapf returns an error annotating err with a stack trace at the point Wrapf is call, and the format specifier. If err is nil, Wrapf returns nil.
Example ¶
package main
import (
"fmt"
"github.com/pkg/errors"
)
func main() {
cause := errors.New("whoops")
err := errors.Wrapf(cause, "oh noes #%d", 2)
fmt.Println(err)
}
Output: oh noes #2: whoops
Types ¶
type BehaviourFunc ¶
BehaviourFunc defines the signature needed for a function to check if an error has a specific behaviour attached.
type Error ¶
type Error string
Error type can be used for constant errors and says nothing about its behaviour. http://dave.cheney.net/2016/04/07/constant-errors
type ErrorFormatFunc ¶
ErrorFormatFunc is a function callback that is called by Error to turn the list of errors into a string.
type Frame ¶
type Frame uintptr
Frame represents a program counter inside a stack frame.
func (Frame) Format ¶
Format formats the frame according to the fmt.Formatter interface.
%s source file %d source line %n function name %v equivalent to %s:%d
Format accepts flags that alter the printing of some verbs, as follows:
%+s path of source file relative to the compile time GOPATH %+v equivalent to %+s:%d
type MultiErr ¶
type MultiErr struct {
Errors []error
Formatter ErrorFormatFunc
}
MultiErr represents a container for collecting and printing multiple errors.
func NewMultiErr ¶
NewMultiErr creates a new multi error struct.
func (*MultiErr) AppendErrors ¶
AppendErrors adds multiple errors to the container. Does not add a location. If *MultiErr is nil it creates a new pointer and returns it.
type StackTrace ¶
type StackTrace []Frame
StackTrace is stack of Frames from innermost (newest) to outermost (oldest).
func (StackTrace) Format ¶
func (st StackTrace) Format(s fmt.State, verb rune)
Format formats the stack of Frames according to the fmt.Formatter interface.
%s lists source files for each Frame in the stack %v lists the source file and line number for each Frame in the stack
Format accepts flags that alter the printing of some verbs, as follows:
%+v Prints filename, function, and line number for each Frame in the stack.