errorc adds typed, inspectable context to Go errors. Written by ygrebnov.
Use fmt.Errorf when a readable message and %w wrapping are enough. With errorc, context such as an ID, duration, or component remains a typed field that callers can retrieve without parsing Error() text. WithCause also pairs a reusable category error with a concrete cause; errors.Is and errors.As can find both.
var ErrInvalidConfiguration = errorc.New("invalid configuration") // Define once.
cause := errors.New("port must be positive")
err := errorc.With(
ErrInvalidConfiguration,
errorc.String("component", "server"),
errorc.Int("port", -1),
errorc.Error("cause", cause),
)
fmt.Println(err) // invalid configuration, cause: port must be positive, component: server, port: -1
fmt.Println(errors.Is(err, ErrInvalidConfiguration), errors.Is(err, cause)) // true true
port, ok := errorc.Lookup[int](err, "port")
fmt.Println(port, ok) // -1 true
// For simple error wrapping, use WithCause:
err = errorc.WithCause(ErrInvalidConfiguration, cause)
fmt.Println(err) // invalid configuration, cause: port must be positiveWith(err, fields...) adds fields to an error. WithCause(category, cause) is shorthand for With(category, Error("cause", cause)). A nil field or cause is ignored; a nil base error yields nil. An Error field adds its original error to the error tree.
| Constructor | Stored value |
|---|---|
String |
string |
Int |
int |
Int64 |
int64 |
Bool |
bool |
Duration |
time.Duration |
Error |
error, also added as a cause |
Fields(err) lists fields across the error tree. Lookup[T](err, key) returns the last field with that key if its value is T; ValueAs[T](field) reads a field directly. Duration renders with time.Duration.String(). All constructors accept plain strings and named string keys, including keys.Key.
Custom types can implement Field (Key, Kind, String) and a typed Value() T method for Lookup and ValueAs. If they also implement Unwrap() error, their cause joins the error tree.
Error() renders each field as key: value, separated by commas; an empty key renders only the value. Cause fields create a multi-error tree, so use errors.Is or errors.As to inspect causes.
Compatible with Go 1.22 or later:
go get github.com/ygrebnov/errorcThis library is pre-1.0; minor version bumps may include breaking changes.
Contributions are welcome. Open an issue or submit a pull request.
Distributed under the BSD 3-Clause License. See LICENSE.