Categorygithub.com/go-faster/errors
modulepackage
0.7.1
Repository: https://github.com/go-faster/errors.git
Documentation: pkg.go.dev

# README

errors Go Reference codecov

Fork of xerrors with explicit Wrap instead of %w.

Clear is better than clever.

go get github.com/go-faster/errors
errors.Wrap(err, "message")

Why

  • Using Wrap is the most explicit way to wrap errors
  • Wrapping with fmt.Errorf("foo: %w", err) is implicit, redundant and error-prone
  • Parsing "foo: %w" is implicit, redundant and slow
  • The pkg/errors and xerrors are not maintainted
  • The cockroachdb/errors is too big
  • The errors has no caller stack trace

Don't need traces?

Call errors.DisableTrace or use build tag noerrtrace.

Additional features

Into

Generic type assertion for errors.

// Into finds the first error in err's chain that matches target type T, and if so, returns it.
//
// Into is type-safe alternative to As.
func Into[T error](err error) (val T, ok bool)
if pathError, ok := errors.Into[*os.PathError](err); ok {
    fmt.Println("Failed at path:", pathError.Path)
}

Must

Must is a generic helper, like template.Must, that wraps a call to a function returning (T, error) and panics if the error is non-nil.

func Must[T any](val T, err error) T

License

BSD-3-Clause, same as Go sources

# Packages

No description provided by the author

# Functions

As finds the first error in err's chain that matches target, and if so, sets target to that error value and returns true.
Caller returns a Frame that describes a frame on the caller's stack.
Cause returns first recorded Frame.
DisableTrace disables capturing caller frames.
Errorf creates new error with format.
FormatError calls the FormatError method of f with an errors.Printer configured according to s and verb, and writes the result to s.
Into finds the first error in err's chain that matches target type T, and if so, returns it.
Is reports whether any error in err's chain matches target.
Join returns an error that wraps the given errors.
Must is a generic helper, like template.Must, that wraps a call to a function returning (T, error) and panics if the error is non-nil.
New returns an error that formats as the given text.
Opaque returns an error with the same error formatting as err but that does not match err and cannot be unwrapped.
Trace reports whether caller stack capture is enabled.
Unwrap returns the result of calling the Unwrap method on err, if err's type contains an Unwrap method returning error.
Wrap error with message and caller.
Wrapf wraps error with formatted message and caller.

# Structs

A Frame contains part of a call stack.

# Interfaces

A Formatter formats error messages.
A Printer formats error messages.
A Wrapper provides context around another error.