errors

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2025 License: MIT Imports: 11 Imported by: 0

README

errors - Error handling primitives

Go Reference

This package extends pkg/errors and xerror packages with additional functionality.

Documentation

Overview

Package errors provides error handling primitives.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target any) bool

As is a proxy for the errors.As function.

func Cause

func Cause(err error) error

Cause is an alias for UnwrapAll.

func Errorf

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

Errorf returns an error that formats according to a format specifier.

func FormatError added in v0.6.0

func FormatError(err error, s fmt.State, verb rune)

FormatError calls the FormatError method of f with a Printer configured according to s and verb, and writes the result to s.

func Into

func Into[T error](err error) (target T, ok bool)

Into finds the first error in err's tree that matches target type T, and if one is found, returns it and true. Otherwise, it returns T's zero value and false.

Into is a type-safe alternative to As.

func Is

func Is(err, target error) bool

Is is a proxy for the errors.Is function.

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 Kind

func Kind(err error) error

Kind finds the first error in err's tree that implements Packer, and if one is found, returns the result of calling its Kind method. Otherwise, Kind returns nil.

func New

func New(msg string) error

New returns an error that formats as the given text.

func Opaque added in v0.6.0

func Opaque(err error) error

Opaque returns an error with the same error formatting as err but that does not match err and cannot be unwrapped. If err is nil, Opaque returns nil.

func Pack

func Pack(kind error, args ...any) error

Pack creates a new error with a given kind. List of valid function signatures:

Pack(kind error)
Pack(kind error, err error)
Pack(kind error, err error, s string)
Pack(kind error, err error, format string, args ...any)
Pack(kind error, s string)
Pack(kind error, format string, args ...any)

If kind is nil, Pack acts as Wrap, Wrapf, New or Errorf depending on the function signature.

If kind is nil and args are empty, Pack returns nil.

func SetTrace

func SetTrace(enable bool)

SetTrace enables/disables automatic stack trace capture.

func Trace

func Trace() bool

Trace reports whether automatic stack trace capture is enabled.

func Unpack added in v0.5.0

func Unpack(err error) error

Unpack finds the first error in err's tree that implements Packer, and if one is found, returns the result of calling its Unpack method. Otherwise, Unpack returns nil.

func Unwrap

func Unwrap(err error) error

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

Unwrap only calls methods of form "Unwrap() error" or "Cause() error", so it doesn't unwrap multi-errors. In particular Unwrap does not unwrap errors returned by Join.

func UnwrapAll added in v0.6.0

func UnwrapAll(err error) error

UnwrapAll recursively unwraps an error until it reaches the underlying cause and returns it.

UnwrapAll only calls methods of form "Unwrap() error" or "Cause() error", so it doesn't unwrap multi-errors. In particular UnwrapAll does not unwrap errors returned by Join.

func UnwrapMulti added in v0.6.0

func UnwrapMulti(err error) []error

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

UnwrapMulti only calls a method of the form "Unwrap() []error".

func WithStack added in v0.4.0

func WithStack(err error) error

WithStack wraps an error with a stack trace.

func Wrap

func Wrap(err error, msg string) error

Wrap wraps an error with the given message. If err is nil, Wrap returns nil.

func Wrapf

func Wrapf(err error, format string, args ...any) error

Wrapf wraps an error with the given formatted message. If err is nil, Wrapf returns nil.

Types

type Formatter added in v0.6.0

type Formatter interface {
	error

	// FormatError prints the receiver's first error and returns the next error in
	// the error chain, if any.
	FormatError(p Printer) (next error)
}

A Formatter formats error messages.

type MultiFormatter added in v0.6.0

type MultiFormatter interface {
	error

	// FormatError prints the receiver's error and returns a slice of next errors
	// in the error chain, if any.
	FormatError(p Printer) (next []error)
}

A MultiFormatter formats multi-error messages.

type MultiWrapper added in v0.6.0

type MultiWrapper interface {
	// Unwrap returns underlying errors.
	Unwrap() []error
}

MultiWrapper provides an additional context around multiple errors.

type Packer added in v0.5.0

type Packer interface {
	// Kind returns the categorical error type.
	Kind() error

	// Unpack returns an error without the categorical error type.
	// If there is nothing except the categorical error type, Unpack returns nil.
	Unpack() error
}

Packer represents an error that can be categorized by kind.

type Printer added in v0.6.0

type Printer interface {
	// Print appends args to the message output.
	Print(args ...any)

	// Printf writes a formatted string.
	Printf(format string, args ...any)

	// Detail reports whether error detail is requested.
	// After the first call to Detail, all text written to the Printer
	// is formatted as additional detail, or ignored when
	// detail has not been requested.
	// If Detail returns false, the caller can avoid printing the detail at all.
	Detail() bool
}

A Printer formats error messages.

The most common implementation of Printer is the one provided by package fmt during Printf (as of Go 1.13). Localization packages such as golang.org/x/text/message typically provide their own implementations.

type StackFrame added in v0.6.0

type StackFrame runtime.Frame

StackFrame is runtime.Frame that implements fmt.Formatter.

func (StackFrame) Format added in v0.6.0

func (sf StackFrame) Format(s fmt.State, verb rune)

Format formats the frame according to the fmt.Formatter interface.

%f    source file
%d    source line
%n    function name
%s    equivalent to %n %f:%d
%v    equivalent to %s

Format accepts flags that alter the printing of some verbs, as follows:

%+v   equivalent to %n\n    %f:%d

func (StackFrame) MarshalText added in v0.6.0

func (sf StackFrame) MarshalText() ([]byte, error)

MarshalText formats a stack frame as a text string. The output is the same as that of fmt.Sprintf("%n %f:%d", f).

func (StackFrame) String added in v0.6.0

func (sf StackFrame) String() string

String formats a stack frame as a text string. The output is the same as that of fmt.Sprintf("%n %f:%d", f).

type StackTrace

type StackTrace []uintptr

StackTrace contains PC values returned by runtime.Callers.

func GetStackTrace added in v0.6.0

func GetStackTrace(err error) StackTrace

GetStackTrace finds the first error in err's tree that implements StackTracer, and if one is found, returns the result of calling its StackTrace method. Otherwise, GetStackTrace returns nil.

func (StackTrace) Format

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

Format formats the stack trace according to the fmt.Formatter interface.

%s    prints each frame as "func file:line"
%v    equivalent to %s

Format accepts flags that alter the printing of some verbs, as follows:

%+v   prints each frame as "func\n    file:line"

func (StackTrace) Frames added in v0.3.0

func (st StackTrace) Frames() []StackFrame

Frames returns a slice of caller frames from st. Wraps runtime.CallersFrames.

func (StackTrace) FramesSeq added in v0.3.0

func (st StackTrace) FramesSeq() iter.Seq[StackFrame]

FramesSeq returns go1.23 iterator that wraps runtime.CallersFrames.

type StackTracer added in v0.6.0

type StackTracer interface {
	// StackTrace returns the stack trace associated with the error.
	StackTrace() StackTrace
}

StackTracer represents an error with an associated stack trace.

type Wrapper added in v0.5.0

type Wrapper interface {
	// Unwrap returns the underlying error.
	Unwrap() error
}

Wrapper provides an additional context around another error.

Jump to

Keyboard shortcuts

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