luminyra

errs overview

Attach structured log attributes to Go error chains, read values with typed keys, and retain stack traces.

Use errs to carry error context across service layers. An Error records a message, cause, creation time, and stack trace. It can also hold standard-library log attributes. New creates an error, and Wrap adds details about the current operation to an existing error.

Installation

You need Go 1.27 or later. Run the installation command in your Go module.

go get github.com/luminyra/errs

Error chains and attributes

  • Key gives an attribute a value type. The predefined Code holds a machine-readable error code, and Public holds a message that can be shown to users.
  • Error.Attrs merges attributes across the error chain. When a name appears more than once, the inner attribute wins because it is closer to the source of the error.
  • Standard-library errors.Is and errors.As work with the wrapped chain. Error.Stack provides an iterator over stack frames.

Go standard library integration

errs works with errors.Is, errors.As, and %w and uses standard-library slog.Attr values. Error implements slog.LogValuer so structured logging can expand the attributes. The handler still controls serialization and output format.

Performance

Creating an error with a stack trace takes about 191 ns. Reading an attribute from a three-level chain with Key.From takes about 19 ns without allocating. Logging an error through a JSON handler takes about 0.8 µs. The stack is formatted on the first log call and cached. Capturing it accounts for most of the cost of creating an error, so create errs errors on failure paths.

The quickstart shows how to wrap a lower-level error and recover its code.

References

On this page