Categorygithub.com/bobg/errors
modulepackage
1.1.0
Repository: https://github.com/bobg/errors.git
Documentation: pkg.go.dev

# README

Errors - drop-in replacement for the stdlib errors package that adds wrapping and call stacks

Go Reference Go Report Card Tests Coverage Status

This is errors, a drop-in replacement for the standard Go errors package. It adds an API for wrapping an error with a caller-specified message and a snapshot of the call stack. It also adds a function for traversing an error’s tree of wrapped sub-errors.

This module began as a fork of the venerable github.com/pkg/errors. That module’s GitHub repository has been frozen for a long time and has not kept up with changes to the standard library. (For example, it does not export the Join function added in Go 1.20.)

This is now a brand-new implementation with a different API.

Usage

When you write code that handles an error from a function call by returning the error to your caller, it’s a good practice to “wrap” the error with context from your callsite first. The Go standard library lets you do this like so:

if err := doThing(...); err != nil {
  return fmt.Errorf("doing thing: %w", err)
}

The resulting error still “is” err (in the sense of errors.Is) but is now wrapped with context about how err was produced − to wit, “doing thing.”

This library lets you do the same thing like this:

if err := doThing(...); err != nil {
  return errors.Wrap(err, "doing thing")
}

This approach has a couple of benefits over wrapping with fmt.Errorf and %w. First, as a convenience, Wrap returns nil if its error argument is nil. So if doThing is the last thing that happens in your function, you can safely write:

err := doThing(...)
return errors.Wrap(err, "doing thing")

This will correctly return nil when doThing succeeds.

Second, errors produced by this package contain a snapshot of the call stack from the moment the error was created. This can be retrieved with the Stack function.

Note that diligent error wrapping often makes the stack trace superfluous. As errors work their way up the stack it’s possible to decorate them with enough extra context to understand exactly where and why the error occurred. But you can’t always rely on diligent error wrapping, and you can’t always wait for an error to propagate all the way up the call stack before reporting it.

# Functions

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.
Errorf is a synonym for [Newf].
Is reports whether any error in err's tree matches target.
Join returns an error that wraps the given errors.
New returns an error that formats as the given text.
Newf creates a new error with the given format string and arguments, formatted as with [fmt.Errorf].
Stack returns the stack trace from an error as a [Frames].
Unwrap returns the result of calling the Unwrap method on err, if err's type contains an Unwrap method returning error.
Walk walks the error tree of e, calling f on each error found.
Wrap creates a wrapped error.
Wrapf creates a wrapped error.

# Variables

ErrSkip can be used by the callback to [Walk] to skip an error's subtree without aborting the walk.
ErrUnsupported indicates that a requested operation cannot be performed, because it is unsupported.

# Structs

Frame is a stack frame.

# Interfaces

Stacker is the interface implemented by errors with a stack trace.

# Type aliases

Frames is a slice of [Frame].