{"modulePath":"github.com/bobg/errors","version":"v1.4.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package errors\n\nPackage errors is a drop-in replacement for the stdlib errors package. It adds error-wrapping functions, stack traces, error-tree traversal.\n\n## Variables\n\n```go\nvar ErrSkip = New(\"skip\")\n```\nErrSkip can be used by the callback to [Walk](#Walk) to skip an error's subtree without aborting the walk.\n\n```go\nvar ErrUnsupported = errors.ErrUnsupported\n```\nErrUnsupported indicates that a requested operation cannot be performed, because it is unsupported. For example, a call to [os.Link](/os#Link) when using a file system that does not support hard links.\n\nFunctions and methods should not return this error but should instead return an error including appropriate context that satisfies\n\n\terrors.Is(err, errors.ErrUnsupported)\n\neither by directly wrapping ErrUnsupported or by implementing an Is method.\n\nFunctions and methods should document the cases in which an error wrapping this will be returned.\n\n## Functions\n\n```go\nfunc As(err error, target any) bool\n```\nAs finds the first error in err's tree that matches target, and if one is found, sets target to that error value and returns true. Otherwise, it returns false.\n\nThe tree consists of err itself, followed by the errors obtained by repeatedly calling Unwrap. When err wraps multiple errors, As examines err followed by a depth-first traversal of its children.\n\nAn error matches target if the error's concrete value is assignable to the value pointed to by target, or if the error has a method As(any) bool such that As(target) returns true. In the latter case, the As method is responsible for setting target.\n\nAn error type might provide an As method so it can be treated as if it were a different error type.\n\nAs panics if target is not a non-nil pointer to either a type that implements error, or to any interface type.\n\n```go\nfunc AsType[E error](err error) (E, bool)\n```\nAsType finds the first error in err's tree that matches the type E, and if one is found, returns that error value and true. Otherwise, it returns the zero value of E and false.\n\nThe tree consists of err itself, followed by the errors obtained by repeatedly calling its Unwrap() error or Unwrap() \\[]error method. When err wraps multiple errors, AsType examines err followed by a depth-first traversal of its children.\n\nAn error err matches the type E if the type assertion err.(E) holds, or if the error has a method As(any) bool such that err.As(target) returns true when target is a non-nil \\*E. In the latter case, the As method is responsible for setting target.\n\n```go\nfunc Errorf(format string, args ...any) error\n```\nErrorf is a synonym for [Newf](#Newf).\n\n```go\nfunc Is(err, target error) bool\n```\nIs reports whether any error in err's tree matches target.\n\nThe tree consists of err itself, followed by the errors obtained by repeatedly calling Unwrap. When err wraps multiple errors, Is examines err followed by a depth-first traversal of its children.\n\nAn error is considered to match a target if it is equal to that target or if it implements a method Is(error) bool such that Is(target) returns true.\n\nAn error type might provide an Is method so it can be treated as equivalent to an existing error. For example, if MyError defines\n\n\tfunc (m MyError) Is(target error) bool { return target == fs.ErrExist }\n\nthen Is(MyError{}, fs.ErrExist) returns true. See [syscall.Errno.Is](/syscall#Errno.Is) for an example in the standard library. An Is method should only shallowly compare err and the target and not call Unwrap on either.\n\n```go\nfunc Join(errs ...error) error\n```\nJoin returns an error that wraps the given errors. Any nil error values are discarded. Join returns nil if every value in errs is nil. The error formats as the concatenation of the strings obtained by calling the Error method of each element of errs, with a newline between each string.\n\nA non-nil error returned by Join implements the Unwrap() \\[]error method.\n\n```go\nfunc New(text string) error\n```\nNew returns an error that formats as the given text. Each call to New returns a distinct error value even if the text is identical. The resulting error also includes a stack trace from the point at which New was called.\n\n```go\nfunc Newf(format string, args ...any) error\n```\nNewf creates a new error with the given format string and arguments, formatted as with [fmt.Errorf](/fmt#Errorf). The result is a wrapped error containing the message and a stack trace from the point at which Newf was called. The format specifier %w works as in [fmt.Errorf](/fmt#Errorf).\n\n```go\nfunc Prune(tree error, targets ...error) error\n```\nPrune returns a copy of the error tree rooted at tree, minus the smallest subtrees matching any of the targets. It answers the question, \"What errors are left after removing the targets?\"\n\nSo, for example, Prune(Join(a, b, c), b) returns Join(a, c). Prune(Wrap(a, x), a) returns nil (since after removing a, there’s nothing left for x to wrap).\n\nNormally, Prune(Wrap(a, x), b) returns a (when a is not b), losing the wrapping info x. But if Wrap(a, x) is a [Rewrapper](#Rewrapper), then the wrapping info is preserved. All errors produced by this package are Rewrappers.\n\n```go\nfunc Unwrap(err error) error\n```\nUnwrap returns the result of calling the Unwrap method on err, if err's type contains an Unwrap method returning error. Otherwise, Unwrap returns nil.\n\nUnwrap only calls a method of the form \"Unwrap() error\". In particular Unwrap does not unwrap errors returned by [Join](#Join).\n\n```go\nfunc Walk(e error, f func(error) error) error\n```\nWalk walks the error tree of e, calling f on each error found. If f returns a non-nil error, Walk stops and returns that error, unless the error is [ErrSkip](#ErrSkip), in which case the walk skips e's children but otherwise continues without error.\n\nThe error tree of e consists of e itself and any children obtained by calling e's optional Unwrap method (whose return type may be error or \\[]error), recursively.\n\nThe nodes of this tree are visited in a preorder, depth-first traversal.\n\n```go\nfunc Wrap(err error, msg string) error\n```\nWrap creates a wrapped error. It wraps the given error and attaches the given message. If err does not already contain a stack trace, one is added from the point at which Wrap was called. If err is nil, Wrap returns nil.\n\n```go\nfunc Wrapf(err error, format string, args ...any) error\n```\nWrapf creates a wrapped error. It wraps the given error and attaches the message that results from formatting format with args. If err does not already contain a stack trace, one is added from the point at which Wrap was called. The format string may include %w specifiers, which work as in [fmt.Errorf](/fmt#Errorf). If any errors included via %w include a stack trace, that will also prevent Wrapf from including a new one. If err is nil, Wrapf returns nil.\n\n## Types\n\n```go\ntype Frame struct {\n\tFunction, File string\n\tLine           int\n}\n```\nFrame is a stack frame.\n\n```go\nfunc (f Frame) String() string\n```\n\n```go\ntype Frames []Frame\n```\nFrames is a slice of [Frame](#Frame).\n\n```go\nfunc Stack(e error) Frames\n```\nStack returns the stack trace from an error as a [Frames](#Frames). If the error is nil or does not contain a stack trace, Stack returns nil.\n\n```go\nfunc (fs Frames) String() string\n```\n\n```go\ntype MultiUnwrapper interface {\n\tUnwrap() []error\n}\n```\nUnwrapper is an optional interface that an error may implement. The Unwrap method returns the list of errors that this error wraps.\n\n```go\ntype Rewrapper interface {\n\tRewrap(...error) error\n}\n```\nRewrapper is an optional interface that an [Unwrapper](#Unwrapper) or [MultiUnwrapper](#MultiUnwrapper) may implement. The Rewrap method wraps the given errors with the same information that the original error wrapped its nested error. A type that wraps only a single nested error may implement Rewrap by calling [Join](#Join) on the given error list.\n\n```go\ntype Stacker interface {\n\tStack() []uintptr\n}\n```\nStacker is the interface implemented by errors with a stack trace. Types wishing to implement Stacker can use [runtime.Callers](/runtime#Callers) to get the call stack.\n\n```go\ntype Unwrapper interface {\n\tUnwrap() error\n}\n```\nUnwrapper is an optional interface that an error may implement. The Unwrap method returns the nested error that this error wraps.\n\n","path":"github.com/bobg/errors","name":"errors","synopsis":"Package errors is a drop-in replacement for the stdlib errors package.","isRedistributable":true}
