Documentation ¶
Overview ¶
Package errors provides a set of error handling helpers, extending the standard library errors package.
Index ¶
- Variables
- func As(err error, target any) bool
- func CallerInfo() string
- func Ignore1[T any](v T, err error) T
- func Ignore2[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2)
- func Is(err, target error) bool
- func Join(errs ...error) error
- func Log(err error) error
- func Log1[T any](v T, err error) T
- func Log2[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2)
- func Must(err error)
- func Must1[T any](v T, err error) T
- func Must2[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2)
- func New(text string) error
- func Unwrap(err error) error
Constants ¶
This section is empty.
Variables ¶
var ErrUnsupported = errors.ErrUnsupported
ErrUnsupported indicates that a requested operation cannot be performed, because it is unsupported. For example, a call to os.Link when using a file system that does not support hard links.
Functions and methods should not return this error but should instead return an error including appropriate context that satisfies
errors.Is(err, errors.ErrUnsupported)
either by directly wrapping ErrUnsupported or by implementing an Is method.
Functions and methods should document the cases in which an error wrapping this will be returned.
Functions ¶
func As ¶
As 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.
The 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, As examines err followed by a depth-first traversal of its children.
An 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(interface{}) bool such that As(target) returns true. In the latter case, the As method is responsible for setting target.
An error type might provide an As method so it can be treated as if it were a different error type.
As panics if target is not a non-nil pointer to either a type that implements error, or to any interface type.
func CallerInfo ¶
func CallerInfo() string
CallerInfo returns string information about the caller of the function that called CallerInfo.
func Ignore1 ¶
Ignore1 ignores an error return value for a function returning a value and an error, allowing direct usage of the value. The intended usage is:
a := errors.Ignore1(MyFunc(v))
func Ignore2 ¶
Ignore2 ignores an error return value for a function returning two values and an error, allowing direct usage of the values. The intended usage is:
a, b := errors.Ignore2(MyFunc(v))
func Is ¶
Is reports whether any error in err's tree matches target.
The 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, Is examines err followed by a depth-first traversal of its children.
An 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.
An error type might provide an Is method so it can be treated as equivalent to an existing error. For example, if MyError defines
func (m MyError) Is(target error) bool { return target == fs.ErrExist }
then Is(MyError{}, fs.ErrExist) returns true. See 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.
func Join ¶
Join 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.
A non-nil error returned by Join implements the Unwrap() []error method.
func Log ¶
Log takes the given error and logs it if it is non-nil. The intended usage is:
errors.Log(MyFunc(v)) // or return errors.Log(MyFunc(v))
func Log1 ¶
Log1 takes the given value and error and returns the value if the error is nil, and logs the error and returns a zero value if the error is non-nil. The intended usage is:
a := errors.Log1(MyFunc(v))
func Log2 ¶
Log2 takes the given two values and error and returns the values if the error is nil, and logs the error and returns zero values if the error is non-nil. The intended usage is:
a, b := errors.Log2(MyFunc(v))
func Must ¶
func Must(err error)
Must takes the given error and panics if it is non-nil. The intended usage is:
errors.Must(MyFunc(v))
func Must1 ¶
Must1 takes the given value and error and returns the value if the error is nil, and panics if the error is non-nil. The intended usage is:
a := errors.Must1(MyFunc(v))
func Must2 ¶
Must2 takes the given two values and error and returns the values if the error is nil, and panics if the error is non-nil. The intended usage is:
a, b := errors.Must2(MyFunc(v))
func New ¶
New returns an error that formats as the given text. Each call to New returns a distinct error value even if the text is identical.
Types ¶
This section is empty.