errors

package
v1.19.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package errors replaces the standard Go package, adding the ability to attach a stack trace and annotations to an error

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target any) bool

As delegates to the standard Go's errors.As function.

func CatchPanic added in v1.13.1

func CatchPanic(f func() error) (err error)

CatchPanic calls the given function and returns any panic as a standard error.

func Is

func Is(err, target error) bool

Is delegates to the standard Go's errors.Is function.

func Join

func Join(errs ...error) error

Join aggregates multiple errors into one. The stack traces of the original errors are discarded and a new stack trace is captured.

func New

func New(pattern string, args ...any) error

New creates a new error, with a static or formatted message, optionally wrapping another error, attaching a status code or attaching properties.

In the simplest case, the pattern is a static string.

New("network timeout")

If the pattern contains % signs, the next appropriate number of arguments are used to format the message of the new error as if with fmt.Errorf.

New("failed to parse '%s' for user %d", dateStr, userID)

Any additional arguments are treated like slog name=value pairs and added to the error's property bag. Properties are not part of the error's message and can be retrieved up the call stack in a structured way.

New("failed to execute '%s'", cmd,
	"exitCode", exitCode,
	"os", os,
)

Three notable properties do not require a name: errors, integers and 32-character long hex string.

New("failed to parse form",
	err,
	http.StatusBadRequest,
	"ba0da7b3d3150f20702229c4521b58e9",
	"path", r.URL.Path,
)

An unnamed error is interpreted to be the original source of the error. The new error is created to wrap the original error as if with

fmt.Errorf(errorMessage+": %w", originalError)

An unnamed integer is interpreted to be an HTTP status code to associate with the error. If the pattern is empty, the status text is set by default.

An unnamed 32-character long hex string is interpreted to be a trace ID.

func Newc deprecated

func Newc(statusCode int, text string) error

Newc creates a new error with an HTTP status code, capturing the current stack location.

Deprecated: Use New

func Newcf deprecated

func Newcf(statusCode int, format string, a ...any) error

Newcf creates a new formatted error with an HTTP status code, capturing the current stack location.

Deprecated: Use New

func Newf deprecated

func Newf(format string, a ...any) error

Newf formats a new error, capturing the current stack location.

Deprecated: Use New

func RuntimeTrace

func RuntimeTrace(levels int) (file string, function string, line int, ok bool)

RuntimeTrace traces back by the amount of levels to retrieve the runtime information used for tracing.

func StatusCode

func StatusCode(err error) int

StatusCode returns the HTTP status code associated with an error. It is the equivalent of Convert(err).StatusCode. If not specified, the default status code is 500.

func Trace

func Trace(err error, a ...any) error

Trace appends the current stack location to the error's stack trace. The variadic arguments behave like those of New.

func TraceCaller added in v1.13.1

func TraceCaller(err error) error

TraceCaller appends the stack location of the caller to the error's stack trace.

func TraceFull

func TraceFull(err error, level int) error

TraceFull appends the full stack to the error's stack trace, starting at the indicated level. Level 0 captures the location of the caller.

func TraceUp deprecated

func TraceUp(err error, level int) error

TraceUp appends the level above the current stack location to the error's stack trace. Level 0 captures the location of the caller.

Deprecated: Use TraceCaller

func Tracec deprecated

func Tracec(statusCode int, err error) error

Tracec appends the current stack location to the error's stack trace and sets the status code.

Deprecated: Use Trace

func Unwrap

func Unwrap(err error) error

Unwrap delegates to the standard Go's errors.Wrap function.

Types

type StackFrame added in v1.13.1

type StackFrame struct {
	Function string `json:"func"`
	File     string `json:"file"`
	Line     int    `json:"line"`
}

StackFrame is a single stack location.

func (*StackFrame) String added in v1.13.1

func (t *StackFrame) String() string

String returns a string representation of the stack frame.

type StreamedError added in v1.13.1

type StreamedError struct {
	Error      string        `json:"error" jsonschema:"example=message"`
	StatusCode int           `json:"statusCode,omitzero"`
	Trace      string        `json:"trace,omitzero"`
	Stack      []*StackFrame `json:"stack,omitzero"`
}

StreamedError is the schema used to marshal and unmarshal the traced error.

type TracedError

type TracedError struct {
	Err        error
	Stack      []*StackFrame
	StatusCode int
	Trace      string
	Properties map[string]any
}

TracedError is a standard Go error augmented with a stack trace, status code and property bag.

func Convert

func Convert(err error) *TracedError

Convert converts an error to one that supports stack tracing. If the error already supports this, it is returned as it is. Note: Trace should be called to include the error's trace in the stack.

func (*TracedError) Error added in v1.13.1

func (e *TracedError) Error() string

Error returns the error string.

func (*TracedError) Format

func (e *TracedError) Format(s fmt.State, verb rune)

Format the error based on the verb and flag.

func (*TracedError) MarshalJSON

func (e *TracedError) MarshalJSON() ([]byte, error)

MarshalJSON marshals the error to JSON.

func (*TracedError) String

func (e *TracedError) String() string

String returns a human-friendly representation of the traced error.

func (*TracedError) UnmarshalJSON

func (e *TracedError) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals the error from JSON. Neither the type of the error nor any errors it wraps can be restored.

func (*TracedError) Unwrap

func (e *TracedError) Unwrap() error

Unwrap returns the underlying error.

Jump to

Keyboard shortcuts

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