errors

package
v0.0.0-...-91cf1c9 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2023 License: MIT Imports: 9 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target any) bool

As finds the first error in err's chain that matches the type to which target points, and if so, sets the target to its value and returns true. An error matches a type if it is assignable to the target type, or if it has a method As(any) bool such that As(target) returns true. As will panic if target is not a non-nil pointer to a type which implements error or is of interface type.

The As method should set the target to its value and return true if err matches the type to which target points.

Note: this implementation differs from that of xerrors as follows: - it also supports recursing through causes with Cause(). - if it detects an API use error, its panic object is a valid error.

func Cause

func Cause(err error) error

Cause aliases UnwrapAll() for compatibility with github.com/pkg/errors.

func Is

func Is(err, reference error) bool

Is determines whether one of the causes of the given error or any of its causes is equivalent to some reference error.

As in the Go standard library, an error is considered to match a reference error if it is equal to that target or if it implements a method Is(error) bool such that Is(reference) returns true.

Note: the inverse is not true - making an Is(reference) method return false does not imply that errors.Is() also returns false. Errors can be equal because their network equality marker is the same. To force errors to appear different to Is(), use errors.Mark().

Note: if any of the error types has been migrated from a previous package location or a different type, ensure that RegisterTypeMigration() was called prior to Is(). Is determines whether one of the causes of the given error or any of its causes is equivalent to some reference error.

As in the Go standard library, an error is considered to match a reference error if it is equal to that target or if it implements a method Is(error) bool such that Is(reference) returns true.

Note: the inverse is not true - making an Is(reference) method return false does not imply that errors.Is() also returns false. Errors can be equal because their network equality marker is the same. To force errors to appear different to Is(), use errors.Mark().

Note: if any of the error types has been migrated from a previous package location or a different type, ensure that RegisterTypeMigration() was called prior to Is().

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 aliases UnwrapOnce() for compatibility with xerrors.

func UnwrapAll

func UnwrapAll(err error) error

UnwrapAll accesses the root cause object of the error. If the error has no cause (leaf error), it is returned directly.

func UnwrapOnce

func UnwrapOnce(err error) (cause error)

UnwrapOnce accesses the direct cause of the error if any, otherwise returns nil.

It supports both errors implementing causer (`Cause()` method, from github.com/pkg/errors) and `Wrapper` (`Unwrap()` method, from the Go 2 error proposal).

func With

func With(back, front error) error

With returns an error that represents front wrapped over back. If back is nil, the returned error is nil.

Calling Unwrap in a loop on this error will iteratively unwrap the front error first, until it runs out of wrapped errors, and then return the back error. This is also the order that Is and As will read the wrapped errors.

The returned error's message will be the concatenation of the two error strings.

func WithStack

func WithStack(err error) error

WithStack annotates err with a wrapper trace at the point WithStack was called.

func WithStackDepth

func WithStackDepth(err error, depth int) error

WithStackDepth annotates err with a wrapper trace starting from the given call depth. The value zero identifies the caller of WithStackDepth itself. See the documentation of WithStack() for more details.

func Wrap

func Wrap(err error, msg string) error

Wrap wraps an error with a message prefix. A stack trace is retained.

func WrapWithFields

func WrapWithFields(err error, fields Fields) error

WrapWithFields adds fields to an existing error.

func WrapWithFieldsAndDepth

func WrapWithFieldsAndDepth(err error, fields Fields, depth int) error

func Wrapf

func Wrapf(err error, format string, args ...interface{}) error

Wrapf wraps an error with a formatted message prefix. A stack trace is also retained. If the format is empty, no prefix is added, but the extra arguments are still processed for reportable strings.

Types

type Aser

type Aser interface {
	As(target any) bool
}

Aser interface enforces stdlib error As interface

type Fields

type Fields map[string]any

func GetFields

func GetFields(err error) Fields

GetFields retrieves any Fields from a stack of causes, combines them such that the last key value pair wins.

type Iser

type Iser interface {
	Is(err error) bool
}

Iser interface enforces stdlib error Is interface

type Stack

type Stack []uintptr

Stack represents a Stack of program counters. This mirrors the (non-exported) type of the same name in github.com/pkg/errors.

func Callers

func Callers(skip int) *Stack

Callers mirrors the code in github.com/pkg/errors, but makes the skip depth customizable.

func ElideSharedStackSuffix

func ElideSharedStackSuffix(prevStack, newStack *Stack) (*Stack, bool)

ElideSharedStackSuffix removes the suffix of newStack that's already present in prevStack. The function returns true if some entries were elided. type StackTrace []Frame -> type StackTrace runtime.Frames callers []uintptr

func (*Stack) Format

func (s *Stack) Format(st fmt.State, verb rune)

Format mirrors the code in github.com/pkg/errors. https://github.com/pkg/errors/blob/master/stack.go#L142

func (*Stack) StackTrace

func (s *Stack) StackTrace() *StackTrace

StackTrace mirrors the code in github.com/pkg/errors.

type StackTrace

type StackTrace runtime.Frames

StackTrace is Stack of Frames from innermost (newest) to outermost (oldest).

func (*StackTrace) Next

func (st *StackTrace) Next() (_ runtime.Frame, more bool)

Next returns the next frame in the wrapper trace, and a boolean indicating whether there are more after it.

func (*StackTrace) String

func (st *StackTrace) String() string

type Unwrapper

type Unwrapper interface {
	Unwrap() error
}

Unwrapper interface enforces stdlib error Unwrap interface

Jump to

Keyboard shortcuts

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