berr

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2025 License: ISC Imports: 4 Imported by: 0

README

[B]etter [err]or handling in Go

Simple library that focuses on improving the errors in two ways:

[!NOTE] Both berr.New() and berr.WithContext() return error interface, meaning that no function signature needs to change to use this library.

📜 Error trace

Allow errors to be traced using:

// Will result in `nil` if `err == nil`, otherwise in `prefix this error: <err>`
berr.New("prefix this error", err)

You can print a pretty message using berr.Expand(error) or berr.Fexpand(io.Writer, error). Example:

[Error]
another example: Hello World!: Yet another error!

[Trace]
1. "another example"
2. "Hello World!"
3. "Yet another error!"

🖨️ Error context

You can add context to an error, which is a list of variables to be included with the error:

berr.WithContext(
    "Hello World!",
    err,
    "c", "Something Something",
    "d", map[string]bool{"x": true, "y": false, "z": true},
),

You can print a pretty message using berr.Expand(error) or berr.Fexpand(io.Writer, error). Example:

[Error]
another example: Hello World!: Yet another error!

[Trace]
1. "another example"
└── a: 123
└── b: 456
2. "Hello World!"
└── c: "Something Something"
└── d: map[string]bool{"x":true, "y":false, "z":true}
3. "Yet another error!"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Expand

func Expand(err error) string

func Fexpand

func Fexpand(writer io.Writer, err error)

func WithContext

func WithContext(label string, err error, args ...any) error

Types

type BoxedError added in v0.1.1

type BoxedError interface {
	error
	Ignore(errs ...error) BoxedError
}

func New

func New(label string, err error) BoxedError

Jump to

Keyboard shortcuts

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