errors

package
v0.68.15-beta2024111501 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package errors contains helper functions for wrapping errors with stack traces, stack output, and panic recovery.

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 tree that matches target, and if one is found, sets target to that error value and returns true. Otherwise, it returns false.

func ContainsStackTrace

func ContainsStackTrace(err error) bool

ContainsStackTrace returns true if the given error contain the stack trace. Useful to avoid creating a nested stack trace.

func ErrorStack

func ErrorStack(err error) string

ErrorStack returns an stack trace if available.

func Errorf

func Errorf(format string, vals ...any) error

Errorf creates a new error with the given format and values. It can be used as a drop-in replacement for fmt.Errorf() to provide descriptive errors in return values. If none of the given values contains a stack trace, it will be created.

func Is

func Is(err, target error) bool

Is reports whether any error in err's tree matches target.

func IsContextCanceled

func IsContextCanceled(err error) bool

IsContextCanceled returns `true` if error has occurred by event `context.Canceled` which is not really an error.

func IsError

func IsError(actual error, expected error) bool

IsError returns true if actual is the same type of error as expected. This method unwraps the given error objects (if they are wrapped in objects with a stacktrace) and then does a simple equality check on them.

func Join

func Join(errs ...error) error

Join returns an error that wraps the given errors.

func New

func New(val any) error

New creates a new instance of Error. If the given value does not contain a stack trace, it will be created.

func Recover

func Recover(onPanic func(cause error))

Recover tries to recover from panics, and if it succeeds, calls the given onPanic function with an error that explains the cause of the panic. This function should only be called from a defer statement.

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.

func UnwrapErrors

func UnwrapErrors(err error) []error

UnwrapErrors unwraps all nested multierrors, and errors that were wrapped with `fmt.Errorf("%w", err)`.

func UnwrapMultiErrors

func UnwrapMultiErrors(err error) []error

UnwrapMultiErrors unwraps all nested multierrors into error slice.

Types

type MultiError

type MultiError struct {
	// contains filtered or unexported fields
}

MultiError is an error type to track multiple errors.

func (*MultiError) Append

func (errs *MultiError) Append(appendErrs ...error) *MultiError

Append is a helper function that will append more errors onto an Multierror in order to create a larger errs-error.

func (*MultiError) Error

func (errs *MultiError) Error() string

Error implements the error interface.

func (*MultiError) ErrorOrNil

func (errs *MultiError) ErrorOrNil() error

ErrorOrNil returns an error interface if this Error represents a list of errors, or returns nil if the list of errors is empty.

func (*MultiError) Len

func (errs *MultiError) Len() int

Len implements sort.Interface function for length.

func (*MultiError) Less

func (errs *MultiError) Less(i, j int) bool

Less implements sort.Interface function for determining order.

func (*MultiError) Swap

func (errs *MultiError) Swap(i, j int)

Swap implements sort.Interface function for swapping elements.

func (*MultiError) Unwrap

func (errs *MultiError) Unwrap() []error

func (*MultiError) WrappedErrors

func (errs *MultiError) WrappedErrors() []error

WrappedErrors returns the error slice that this Error is wrapping.

Jump to

Keyboard shortcuts

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