errors

package
v5.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2024 License: MIT Imports: 5 Imported by: 14

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MaxStackDepth = 50

MaxStackDepth the maximum number of frames collected when creating a new Error.

Functions

func Errorf

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

Errorf is a shortcut for `errors.New(fmt.Errorf("format", args))`. Be careful when using this, this will result in losing the callers of the original error if one of the `args` is of type `*errors.Error`.

func New

func New(reason any) error

New create a new `*Error`. Collects the function callers.

If the given reason is already of type `*Error`, returns it without change. If the reason is a slice of `error`, joins them using std's `errors.Join`.

If the given reason is `nil`, returns `nil`. If the reason is `[]error` or `[]any`, the `nil` elements are ignored.

If the reason is anything other than an `error`, `[]error`, `*Error`, `[]*Error`, `[]any`, it will be wrapped in a `Reason` structure, allowing to preserve its JSON marshaling behavior.

func NewSkip

func NewSkip(reason any, skip int) error

NewSkip create a new `*Error`. Collects the function callers, skipping the given amount of frames.

If the given reason is already of type `*Error`, returns it without change. If the reason is a slice of `error`, joins them using std's `errors.Join`.

If the given reason is `nil`, returns `nil`. If the reason is `[]error` or `[]any`, the `nil` elements are ignored.

If the reason is anything other than an `error`, `[]error`, `*Error`, `[]*Error`, `[]any`, it will be wrapped in a `Reason` structure, allowing to preserve its JSON marshaling behavior.

Types

type Error

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

Error wraps an errors and attaches callers at the time of creation. This implementation provides information for debugging or error reporting. It is encouraged to use this type of error everywhere a function can return an unexpected error. Functions returning an error that is only indicative should not use this error type (e.g.: user errors).

func (Error) Callers

func (e Error) Callers() []uintptr

Callers returns the function callers collected at the time of creation of the `Error`.

func (Error) Error

func (e Error) Error() string

func (Error) FileLine

func (e Error) FileLine() string

FileLine returns the file path and line of the error.

func (Error) Len

func (e Error) Len() int

Len returns the number of underlying reasons.

func (Error) MarshalJSON

func (e Error) MarshalJSON() ([]byte, error)

MarshalJSON marshals the error and its underlying reasons. The result will be: - a string if the reasons slice is empty - the marshaled first reason if the reasons slice contains only one error - an array of the marshaled reasons otherwise

func (Error) StackFrames

func (e Error) StackFrames() FrameStack

StackFrames returns the parsed `FrameStack` for this error.

func (Error) String

func (e Error) String() string

func (Error) Unwrap

func (e Error) Unwrap() []error

type FrameStack

type FrameStack []runtime.Frame

FrameStack slice of frames containing information about the stack. Can be used to generate a stack trace for debugging, or for error reporting.

func (FrameStack) String

func (s FrameStack) String() string

type Reason

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

Reason wrapper around any type of error Reason. This allows json marshaling of the Reason instead of losing the original data using `%v` format. Calling `Error()` on this structure returns the original data formatted with `%v`.

func (Reason) Error

func (r Reason) Error() string

func (Reason) MarshalJSON

func (r Reason) MarshalJSON() ([]byte, error)

MarshalJSON marshals the wrapped reason.

func (Reason) Value

func (r Reason) Value() any

Value returns the reason's value.

Jump to

Keyboard shortcuts

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