e

package
v0.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 22, 2025 License: MIT Imports: 4 Imported by: 3

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

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

func From(origin error, f ...fields.Field) *Err

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

func New(reason string, f ...fields.Field) *Err

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

func NewFrom(reason string, wrapped error, f ...fields.Field) *Err

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

func (e *Err) Clone() *Err

Clone returns a new Err with the same error, wrapped error, and a cloned fields container.

func (*Err) Error

func (e *Err) Error() string

Error returns the string representation of the Err, including reason, fields, and wrapped errors.

func (*Err) Fields

func (e *Err) Fields() fields.List

Fields returns the metadata fields attached to the Err.

func (*Err) Reason

func (e *Err) Reason() string

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) Unwrap deprecated

func (e *Err) Unwrap() []error

Unwrap returns the underlying errors for compatibility with errors.Is and errors.As.

Deprecated: This method is for internal use only. Prefer using the errors package directly.

func (*Err) WithField

func (e *Err) WithField(key string, val any) *Err

WithField returns a new Err with the same error and a single additional field.

func (*Err) WithFields

func (e *Err) WithFields(f ...fields.Field) *Err

WithFields returns a new Err with the same error and the provided fields.

func (*Err) Wrap

func (e *Err) Wrap(err error, f ...fields.Field) *Err

Wrap returns a new Err that wraps the provided error with the current Err as context. Additional fields can be attached. If err is nil, it is replaced with an Err for "error(nil)".

type ErrorLogger added in v0.2.0

type ErrorLogger func(msg string, err error, fs ...fields.Field)

ErrorLogger defines a function that logs an error message, an error, and optional fields.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL