Documentation
¶
Overview ¶
Package e provides a custom error type with support for error chaining and structured metadata fields.
The main type, Err, enables convenient error wrapping and the attachment of key-value metadata fields (fields.Field). Errors can be wrapped to add context and enriched with fields for structured logging or diagnostics.
Example: Wrapping errors with context
var ErrJSONParseFailed = e.New("JSON parse failed") var val any if err := json.Unmarshal([]byte(`["invalid", "json]`), &val); err != nil { return ErrJSONParseFailed.Wrap(err) // Output: "JSON parse failed: unexpected end of JSON input" }
Example: Enriching errors with fields
err := e.New("operation failed").WithField("user_id", 42) // Output: "operation failed (user_id=42)" err = err.Wrap(e.New("db error").WithFields(fields.F("query", "SELECT *"))) // Output: "operation failed (user_id=42): db error (query=SELECT *)"
Any error can be converted to an Err using the From function. This does not wrap the error; unwrapping will not return the original error.
e.From(errors.New("error")) // "error" errors.Unwrap(e.From(errors.New("error"))) // nil
The Err string format is:
<reason> (fields...): <wrapped error string>
Fields are always enclosed in parentheses, and wrapped errors are separated by a colon and space.
All methods that return errors create new instances; errors are immutable.
Deprecated methods Unwrap, Is, and As are present for compatibility with the errors package, but should not be used directly. Use the errors package functions instead.
The Log function logs errors using our logger abstraction - logger.Logger, extracting reason, wrapped error, and fields, logging them appropriately.
Index ¶
- func Log(err error, f ErrorLogger)
- type Err
- func (e *Err) Clone() *Err
- func (e *Err) Error() string
- func (e *Err) Fields() fields.List
- func (e *Err) Reason() string
- func (e *Err) Unwrap() []errordeprecated
- func (e *Err) WithField(key string, val any) *Err
- func (e *Err) WithFields(f ...fields.Field) *Err
- func (e *Err) Wrap(err error, f ...fields.Field) *Err
- type ErrorLogger
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Log ¶ added in v0.2.0
func Log(err error, f ErrorLogger)
Log logs the provided error using the given ErrorLogger function.
If err is nil, Log does nothing. If err is of type Err, its reason is used as the log message, the wrapped error is passed as the error, and its fields are passed as log fields. For other error types, err.Error() is used as the message and nil is passed as the error.
Types ¶
type Err ¶
type Err struct {
// contains filtered or unexported fields
}
Err represents a custom error type that supports error chaining and structured metadata fields.
func From ¶
From converts any error to an Err, optionally adding fields. This is not true wrapping; unwrapping will not return the original error. Passing nil results in an Err with reason "error(nil)".
func New ¶
New returns a new Err with the given reason and optional fields. The returned error can be further wrapped or annotated with additional fields.
func NewFrom ¶
NewFrom returns a new Err with the given reason, wrapping the provided error, and optional fields. If wrapped is nil, it behaves like New.
func (*Err) Clone ¶
Clone returns a new Err with the same error, wrapped error, and a cloned fields container.
func (*Err) Error ¶
Error returns the string representation of the Err, including reason, fields, and wrapped errors.
func (*Err) Reason ¶
Reason returns the reason string of the Err, without fields or wrapped errors. If the Err is nil, returns "(*e.Err)(nil)". If empty, returns "(*e.Err)(empty)".
func (*Err) WithField ¶
WithField returns a new Err with the same error and a single additional field.
func (*Err) WithFields ¶
WithFields returns a new Err with the same error and the provided fields.