Documentation
¶
Overview ¶
Package zerr provides utilities for safe error handling in goroutines.
Package zerr provides slog integration for structured error logging.
Package zerr provides stack trace utilities for the error handling library.
Package zerr provides a high-performance error handling library with lazy stack traces, deduplication, and structured metadata.
Index ¶
- func Defer(handler func(error))
- func Log(ctx context.Context, logger *slog.Logger, err error)
- func New(message string) error
- func With(err error, key string, value any) error
- func WithStack(err error) error
- func Wrap(err error, message string) error
- type Error
- func (e *Error) Error() string
- func (e *Error) Format(s fmt.State, verb rune)
- func (e *Error) LogValue() slog.Value
- func (e *Error) Metadata() map[string]any
- func (e *Error) StackTrace() string
- func (e *Error) Unwrap() error
- func (e *Error) With(key string, value any) *Error
- func (e *Error) WithStack() *Error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Defer ¶
func Defer(handler func(error))
Defer recovers from panics in goroutines and converts them to errors.
func New ¶
New creates a new error with the given message.
Example ¶
Keep Examples...
err := New("something went wrong")
fmt.Println(err.Error())
Output: something went wrong
func With ¶
With attaches a key-value pair to an error. If err is already a *Error, it attaches the metadata directly. If err is a standard error, it wraps it to allow attaching metadata.
Example ¶
testErr := New("database error")
err, _ := testErr.(*Error)
err = err.With("table", "users").With("operation", "insert")
fmt.Println(err.Error())
Output: database error
func WithStack ¶
Stack captures the stack trace for the error. If err is already a *Error, it attaches the stack trace directly. If err is a standard error, it wraps it to capture the stack trace.
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error represents an error with optional stack trace and metadata.
func (*Error) Format ¶
Format implements the fmt.Formatter interface to allow for printing stack traces.
func (*Error) Metadata ¶
Metadata returns a copy of the error's metadata as a map. Returns an empty map if there is no metadata attached to the error.
func (*Error) StackTrace ¶
StackTrace returns a formatted stack trace string. Uses lazy formatting - the stack trace is only formatted when this method is called.