Documentation
¶
Overview ¶
Package wrap provides utility functions to wrap errors with extra context.
Index ¶
- func Error(wrapped error, message string) error
- func ErrorWithAttrs(wrapped error, message string, logAttributes ...any) error
- func Errorf(wrapped error, messageFormat string, formatArgs ...any) error
- func Errors(wrapped []error, message string) error
- func ErrorsWithAttrs(wrapped []error, message string, logAttributes ...any) error
- func Errorsf(wrapped []error, messageFormat string, formatArgs ...any) error
- func NewErrorWithAttrs(message string, logAttributes ...any) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Error ¶
Error wraps the given error with a message, to add context to the error.
If you're in a function with a context.Context parameter, consider using hermannm.dev/wrap/ctxwrap.Error instead. See the hermannm.dev/wrap/ctxwrap package docs for why you may want to do this.
The returned error implements the Unwrap method from the standard errors package, so it works with errors.Is and errors.As.
Error string format ¶
The following example:
err := errors.New("duplicate primary key")
wrapped := wrap.Error(err, "database insert failed")
fmt.Println(wrapped)
...produces this error string:
database insert failed - duplicate primary key
Wrapped errors can be nested. Wrapping an already wrapped error adds it to the error list, so this next example:
err := errors.New("duplicate primary key")
inner := wrap.Error(err, "database insert failed")
outer := wrap.Error(inner, "failed to store event")
fmt.Println(outer)
...produces this error string:
failed to store event - database insert failed - duplicate primary key
func ErrorWithAttrs ¶ added in v0.4.0
ErrorWithAttrs wraps the given error with a message and log attributes, to add structured context to the error when it is logged (see below for how to pass attributes).
The returned error implements the following method:
LogAttrs() []slog.Attr
A logging library can check for the existence of this method when an error is logged, to add these attributes to the log output. The hermannm.dev/devlog/log library, which wraps log/slog, does this in its error-aware logging functions.
If you're in a function with a context.Context parameter, consider using hermannm.dev/wrap/ctxwrap.ErrorWithAttrs instead. See the hermannm.dev/wrap/ctxwrap package docs for why you may want to do this.
The returned error also implements the Unwrap method from the standard errors package, so it works with errors.Is and errors.As.
Log attributes ¶
A log attribute (abbreviated "attr") is a key-value pair attached to a log line. You can pass attributes in the following ways:
// Pairs of string keys and corresponding values:
wrap.ErrorWithAttrs(err, "error message", "key1", "value1", "key2", 2)
// slog.Attr objects:
wrap.ErrorWithAttrs(err, "error message", slog.String("key1", "value1"), slog.Int("key2", 2))
// Or a mix of the two:
wrap.ErrorWithAttrs(err, "error message", "key1", "value1", slog.Int("key2", 2))
When outputting logs as JSON (using e.g. slog.JSONHandler), these become fields in the logged JSON object. This allows you to filter and query on the attributes in the log analysis tool of your choice, in a more structured manner than if you were to just use string concatenation.
Error string format ¶
The following example:
err := errors.New("duplicate primary key")
wrapped := wrap.Error(err, "database insert failed")
fmt.Println(wrapped)
...produces this error string:
database insert failed - duplicate primary key
Wrapped errors can be nested. Wrapping an already wrapped error adds it to the error list, so this next example:
err := errors.New("duplicate primary key")
inner := wrap.Error(err, "database insert failed")
outer := wrap.Error(inner, "failed to store event")
fmt.Println(outer)
...produces this error string:
failed to store event - database insert failed - duplicate primary key
func Errorf ¶
Errorf wraps the given error with a formatted message, to add context to the error. It forwards the given message format and args to fmt.Sprintf to construct the message.
If you're in a function with a context.Context parameter, consider using hermannm.dev/wrap/ctxwrap.Errorf instead. See the hermannm.dev/wrap/ctxwrap package docs for why you may want to do this.
The returned error implements the Unwrap method from the standard errors package, so it works with errors.Is and errors.As.
Error string format ¶
The following example:
err := errors.New("unrecognized event type")
wrapped := wrap.Errorf(err, "failed to process event of type '%s'", "ORDER_UPDATED")
fmt.Println(wrapped)
...produces this error string:
failed to process event of type 'ORDER_UPDATED' - unrecognized event type
func Errors ¶
Errors wraps the given errors with a message, to add context to the errors.
If you're in a function with a context.Context parameter, consider using hermannm.dev/wrap/ctxwrap.Errors instead. See the hermannm.dev/wrap/ctxwrap package docs for why you may want to do this.
The returned error implements the Unwrap method from the standard errors package, so it works with errors.Is and errors.As.
Error string format ¶
The following example:
errs := []error{errors.New("invalid timestamp format"), errors.New("id was not UUID")}
wrapped := wrap.Errors(errs, "failed to parse event")
fmt.Println(wrapped)
...produces this error string:
failed to parse event - invalid timestamp format - id was not UUID
When combined with wrap.Error, nested wrapped errors are indented, so this next example:
errs := []error{errors.New("invalid timestamp format"), errors.New("id was not UUID")}
inner := wrap.Errors(errs, "failed to parse event")
outer := wrap.Error(inner, "event processing failed")
fmt.Println(outer)
...produces this error string:
event processing failed - failed to parse event - invalid timestamp format - id was not UUID
func ErrorsWithAttrs ¶ added in v0.4.0
ErrorsWithAttrs wraps the given errors with a message and log attributes, to add structured context to the error when it is logged (see below for how to pass attributes).
The returned error implements the following method:
LogAttrs() []slog.Attr
A logging library can check for the existence of this method when an error is logged, to add these attributes to the log output. The hermannm.dev/devlog/log library, which wraps log/slog, does this in its error-aware logging functions.
If you're in a function with a context.Context parameter, consider using hermannm.dev/wrap/ctxwrap.ErrorsWithAttrs instead. See the hermannm.dev/wrap/ctxwrap package docs for why you may want to do this.
The returned error also implements the Unwrap method from the standard errors package, so it works with errors.Is and errors.As.
Log attributes ¶
A log attribute (abbreviated "attr") is a key-value pair attached to a log line. You can pass attributes in the following ways:
// Pairs of string keys and corresponding values:
wrap.ErrorsWithAttrs(errs, "error message", "key1", "value1", "key2", 2)
// slog.Attr objects:
wrap.ErrorsWithAttrs(errs, "error message", slog.String("key1", "value1"), slog.Int("key2", 2))
// Or a mix of the two:
wrap.ErrorsWithAttrs(errs, "error message", "key1", "value1", slog.Int("key2", 2))
When outputting logs as JSON (using e.g. slog.JSONHandler), these become fields in the logged JSON object. This allows you to filter and query on the attributes in the log analysis tool of your choice, in a more structured manner than if you were to just use string concatenation.
Error string format ¶
The following example:
errs := []error{errors.New("invalid timestamp format"), errors.New("id was not UUID")}
wrapped := wrap.Errors(errs, "failed to parse event")
fmt.Println(wrapped)
...produces this error string:
failed to parse event - invalid timestamp format - id was not UUID
When combined with wrap.Error, nested wrapped errors are indented, so this next example:
errs := []error{errors.New("invalid timestamp format"), errors.New("id was not UUID")}
inner := wrap.Errors(errs, "failed to parse event")
outer := wrap.Error(inner, "event processing failed")
fmt.Println(outer)
...produces this error string:
event processing failed - failed to parse event - invalid timestamp format - id was not UUID
func Errorsf ¶ added in v0.4.0
Errorsf wraps the given errors with a formatted message, to add context to the error. It forwards the given message format and args to fmt.Sprintf to construct the message.
If you're in a function with a context.Context parameter, consider using hermannm.dev/wrap/ctxwrap.Errorsf instead. See the hermannm.dev/wrap/ctxwrap package docs for why you may want to do this.
The returned error implements the Unwrap method from the standard errors package, so it works with errors.Is and errors.As.
Error string format ¶
The following example:
errs := []error{errors.New("invalid timestamp format"), errors.New("id was not UUID")}
wrapped := wrap.Errorsf(errs, "failed to process event of type '%s'", "ORDER_UPDATED")
fmt.Println(wrapped)
...produces this error string:
failed to process event of type 'ORDER_UPDATED' - invalid timestamp format - id was not UUID
func NewErrorWithAttrs ¶ added in v0.4.0
NewErrorWithAttrs returns a new error with the given message, and logging attributes to add structured context to the error when it is logged (see below for how to pass attributes).
The returned error implements the following method:
LogAttrs() []slog.Attr
A logging library can check for the existence of this method when an error is logged, to add these attributes to the log output. The hermannm.dev/devlog/log library, which wraps log/slog, does this in its error-aware logging functions.
If you're in a function with a context.Context parameter, consider using hermannm.dev/wrap/ctxwrap.NewErrorWithAttrs instead. See the hermannm.dev/wrap/ctxwrap package docs for why you may want to do this.
Log attributes ¶
A log attribute (abbreviated "attr") is a key-value pair attached to a log line. You can pass attributes in the following ways:
// Pairs of string keys and corresponding values:
wrap.NewErrorWithAttrs("error message", "key1", "value1", "key2", 2)
// slog.Attr objects:
wrap.NewErrorWithAttrs("error message", slog.String("key1", "value1"), slog.Int("key2", 2))
// Or a mix of the two:
wrap.NewErrorWithAttrs("error message", "key1", "value1", slog.Int("key2", 2))
When outputting logs as JSON (using e.g. slog.JSONHandler), these become fields in the logged JSON object. This allows you to filter and query on the attributes in the log analysis tool of your choice, in a more structured manner than if you were to just use string concatenation.
Types ¶
This section is empty.