Categorygithub.com/jjunac/betterr
repositorypackage
0.0.3
Repository: https://github.com/jjunac/betterr.git
Documentation: pkg.go.dev

# README

BettErr

BettErr is a Go library that enhances error handling by providing stack traces and multiple error formatting styles similar to other programming languages.

Why BettErr?

  • Better Debug Information: Get stack traces for easier debugging
  • Flexible Formatting: Choose the error format that best suits your needs (Go-like, Java-like, and JSON)
  • Simple API: Intuitive methods for creating and handling errors
  • Go Compatibility: Works seamlessly with standard Go errors

Roadmap

  • Put runnable examples in the doc
  • Add a way to disable stacktrace (setting + env ?)

Installation

go get github.com/jjunac/betterr

Usage

// Create a new error with stack trace
err := betterr.New("something went wrong")

// Wrap an existing error with stack trace
plainErr := returnsAnError()
wrappedErr := betterr.Wrap(plainErr)

// Decorate an error with additional context
decoratedErr := betterr.Decorate(err, "failed to process")
// Or with formatting
decoratedErr = betterr.Decoratef(err, "failed to process item %d", 123)

Formatting Errors

BettErr supports multiple formatting styles. The Error() methods of the error use the default formatter (Java style by default).
You can set the default formatter by changing betterr.DefaultFortmatter:

betterr.DefaultFortmatter = &betterr.JavaStyleFormatter{}

Go Style

fmt.Println(formatter.Format(&betterr.GoStyleFormatter{}))
// Output: failed to process: something went wrong

Java Style

fmt.Println(formatter.Format(&betterr.JavaStyleFormatter{}))
// Output:
// failed to process
//     at github.com/myapp.MyFunction (file.go:123)
//     at github.com/myapp.main (main.go:45)
// Caused by: something went wrong
//     at github.com/myapp.OtherFunction (file.go:100)

JSON (useful for monitoring for instance)

fmt.Println(formatter.Format(&betterr.JsonFormatter{}))
// Output:
// {
//     "message": "failed to process",
//     "stack": [
//         {
//             "file": "file.go",
//             "function": "github.com/myapp.MyFunction",
//             "line": 123
//         },
//         {
//             "file": "main.go",
//             "function": "github.com/myapp.main",
//             "line": 45
//         }
//     ],
//     "cause": {
//         "message": "something went wrong",
//         "stack": [
//             {
//                 "file": "file.go",
//                 "function": "github.com/myapp.OtherFunction",
//                 "line": 42
//             }
//         ]
//     }
// }

Benchmark

BettErr is faster than the other error handling libraries (such as eris and errorx), but obviously slower than standard's Go errors, since they don't offer the stack trace feature (and runtime stack unwinding takes time).

goos: darwin goarch: arm64 pkg: github.com/jjunac/betterr/benchmark cpu: Apple M3 Pro

BenchmarkRunsTime per error creation
10-frame stack
---Errors5347176422.65 ns/op
---Betterr1862188614.2 ns/op
---Errorx1578981853.7 ns/op
---Eris4441782824 ns/op
100-frame stack
---Errors2480102479.0 ns/op
---Betterr9385101302 ns/op
---Errorx4017443017 ns/op
---Eris1344148903 ns/op