errors

package module
v0.0.0-...-f17dabf Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2025 License: MIT Imports: 2 Imported by: 0

README

errors
go minimal version go tested version CI Codecov Maintainability Go Report Card License Go Reference

Go (1.18+) library to construct errors with fields for structured logging.

Features:

  • Wrap an error with string prefix
  • Add custom fields to an error
  • Extract all fields from chain of wrapped errors
  • Join several errors into one error (build errors tree)
  • Extract paths to each leaf from the errors tree
  • Logger agnostic

Motivation

When structured logger is used, it's better to have constant error messages. For example, message should not contain ID of your entity. Instead, such additional data should be logged in a separate fields. That makes it easier to search, group and analyse logs.

Bad:

{"level": "error", "message": "can't find order a881ff5c-ef23-4e6c-a505-9b66ee42b779"}

Good:

{"level": "error", "message": "can't find order", "order_id": "a881ff5c-ef23-4e6c-a505-9b66ee42b779"}

Installation

go get github.com/maratori/errors

Usage

TBD

Contribution

You are welcome to create an issue or pull request with improvements and fixes. See guide.

License

MIT License

Documentation

Overview

Package errors provides functions to construct errors with custom fields for structured logging.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendInto

func AppendInto(into *error, err error)

func As

func As(err error, target any) bool

func Errors

func Errors(err error) []error

func Is

func Is(err, target error) bool

func Join

func Join(errs ...error) error

func Unwrap

func Unwrap(err error) error

Types

type ErrorBuilder

type ErrorBuilder struct {
	// contains filtered or unexported fields
}

func Err

func Err(err error) *ErrorBuilder

func New

func New(msg string) *ErrorBuilder

func WithField

func WithField(err error, key string, value any) *ErrorBuilder

func WithFields

func WithFields(err error, fields Fields) *ErrorBuilder

func Wrap

func Wrap(prefix string, err error) *ErrorBuilder

func (*ErrorBuilder) E

func (e *ErrorBuilder) E() error

func (*ErrorBuilder) WithField

func (e *ErrorBuilder) WithField(key string, value any) *ErrorBuilder

func (*ErrorBuilder) WithFields

func (e *ErrorBuilder) WithFields(fields Fields) *ErrorBuilder

func (*ErrorBuilder) Wrap

func (e *ErrorBuilder) Wrap(prefix string) *ErrorBuilder

type Fields

type Fields = map[string]any

func FieldsFromError

func FieldsFromError(err error) Fields

Jump to

Keyboard shortcuts

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