errors

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2024 License: BSD-3-Clause Imports: 4 Imported by: 50

Documentation

Overview

Package errors provides a set of error handling helpers, extending the standard library errors package.

Index

Constants

This section is empty.

Variables

View Source
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

func As(err error, target any) bool

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

func Ignore1[T any](v T, err error) T

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

func Ignore2[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2)

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

func Is(err, target error) bool

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

func Join(errs ...error) error

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

func Log(err error) error

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

func Log1[T any](v T, err error) T

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

func Log2[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2)

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

func Must1[T any](v T, err error) T

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

func Must2[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2)

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

func New(text string) error

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.

func Unwrap

func Unwrap(err error) error

Unwrap returns the result of calling the Unwrap method on err, if err's type contains an Unwrap method returning error. Otherwise, Unwrap returns nil.

Unwrap only calls a method of the form "Unwrap() error". In particular Unwrap does not unwrap errors returned by Join.

Types

This section is empty.

Jump to

Keyboard shortcuts

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