Skip to content

bbfh-dev/lib-berr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

[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!"

About

Simple Go library that adds error trace and context

Topics

Resources

License

Stars

Watchers

Forks

Languages