Documentation
¶
Overview ¶
Package errors provides utilities for creating, wrapping, combining, and inspecting errors.
It is 100% dependency-free and works perfectly with the standard library's errors package (Go 1.20+). Multi-errors support errors.Is, errors.As, and errors.Unwrap out of the box.
Errors vs Leaves ¶
Errors returns the top-level items inside a multi-error or stdlib joined error. For a single fmt.Errorf("%w") chain it returns the outer wrapper as one element — not the inner cause.
Leaves walks each extracted item to the root of its %w chain. Use Leaves when you need every underlying cause (for example, logging or Sentry).
Index ¶
- func Append(err error, errs ...error) error
- func As(err error, target any) bool
- func Count(err error) int
- func Errorf(format string, args ...any) error
- func Errors(err error) []error
- func Flatten(err error) error
- func Is(err, target error) bool
- func Join(errs ...error) error
- func Leaves(err error) []error
- func New(msg string) error
- func Prefix(err error, prefix string) error
- func Unwrap(err error) error
- func WithMessage(err error, msg string) error
- func Wrap(err error, msg string) error
- func Wrapf(err error, format string, args ...any) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Append ¶
Append combines multiple errors into a multi-error. Nested multi-errors and joined errors are flattened. Returns nil if all errors are nil.
func Count ¶ added in v1.0.2
Count returns the number of top-level errors contained in err (see Errors).
func Errors ¶
Errors returns the top-level items inside err. Multi-errors and values produced by errors.Join are flattened one level. A single fmt.Errorf("%w") chain is returned as a one-element slice containing the outer wrapper. Use Leaves to reach root causes.
Example ¶
err := Append(
New("validation failed"),
New("user already exists"),
)
for _, e := range Errors(err) {
fmt.Println(e)
}
Output: validation failed user already exists
func Flatten ¶
Flatten returns a single error if err contains only one underlying error. Otherwise returns the multi-error unchanged.
func Join ¶
Join combines multiple errors into a multi-error. Unlike errors.Join, nested multi-errors and stdlib joined errors are flattened into a single level (same behavior as Append). Returns nil if all errors are nil.
func Leaves ¶ added in v1.0.2
Leaves returns the root cause of each item returned by Errors. For multi-errors this is one leaf per sibling; for a lone %w chain it is the innermost wrapped error.
Example ¶
root := New("connection refused")
wrapped := Wrap(root, "dial failed")
err := Append(wrapped, New("disk full"))
for _, leaf := range Leaves(err) {
fmt.Println(leaf)
}
Output: connection refused disk full
func Prefix ¶
Prefix adds the same prefix to every error inside err (works for both single errors and multi-errors).
Example ¶
err1 := New("permission denied")
err2 := New("disk full")
err := Append(err1, err2)
err = Prefix(err, "backup failed")
fmt.Println(err)
Output: 2 errors occurred: - backup failed: permission denied - backup failed: disk full
func WithMessage ¶
WithMessage adds msg as a sibling error. If err is nil, returns a plain error with msg.
func Wrapf ¶
Wrapf wraps err with a formatted message. If err is nil, returns nil. format must not contain %w (the wrapper is appended automatically). Literal percent signs must be escaped as %%.
Example ¶
err := New("connection refused")
err = Wrapf(err, "failed to dial %s:%d", "db.example.com", 5432)
fmt.Println(err)
Output: failed to dial db.example.com:5432: connection refused
Types ¶
This section is empty.