Skip to content

std/errors

import "github.com/eugenioenko/goalchemy/std/errors"

Package errors implements functions to manipulate errors: creation, wrapping inspection with Is and As, and joining. As requires a target whose static type is a pointer to an interface type or to a type implementing error; the compiler checks it at the call site instead of using reflection.

var ErrUnsupported = New("unsupported operation")

ErrUnsupported indicates that a requested operation cannot be performed, because it is unsupported.

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. An error matches if its concrete value is assignable to the value target points to, or if it has a method As(any) bool returning true. The compiler expands each call for the static type of target, which must be a pointer to an interface type or to a type implementing error. As panics if target is a nil pointer and err is not nil.

func Is(err, target error) bool

Is reports whether any error in err’s tree matches target. The tree is err followed by a depth-first traversal of the errors returned by its Unwrap() error or Unwrap() []error methods. An error matches target if it is equal to target or has a method Is(error) bool returning true.

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.

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(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. It does not unwrap errors returned by Join.