Blunder is a simple, gpt-based and easy-to-use error handling package for golang. It generate typed errors and manage them in centralized way, which reduce the dependency between packages and make error handling more convenient.
- Install package
go get github.com/DenChenn/blunder
- Import into your project
printf '// +build tools\npackage tools\nimport (_ "github.com/DenChenn/blunder")' | gofmt > tools.go go mod tidy
- Initialize Blunder
go run github.com/DenChenn/blunder init <your_dir_path>
- Define your error in
blunder.yaml
according to example ❤️ - Generate all errors
or generate with gpt-based auto-completion
go run github.com/DenChenn/blunder gen
export OPENAI_API_TOKEN=<your_openai_api_token> go run github.com/DenChenn/blunder gen --complete=true
Suppose your errors in blunder.yaml
are defined like this:
details:
- package: alayer
errors:
- code: Err1
#...
- code: Err2
#...
- code: Err3
#...
- package: blayer
errors:
- code: Err1
#...
- code: Err2
#...
- code: Err3
#...
Your error will be generated in given path like this:
<your_dir_path>/errors/
generated/
alayer/
errors.go
blayer/
errors.go
blunder.yaml
Which can be import into your code like this:
if err != nil {
if errors.Is(err, &alayer.Err1) {
//...
}
}
Or you can wrap your error like this:
if errors.Is(err, &pgx.ErrNoRows) {
return &alayer.Err1.Wrap(err)
}
All generated errors implement blunder.OrdinaryError
interface, which contains static methods.
ordinaryError, ok := err.(blunder.OrdinaryError)
if ok {
fmt.Println(ordinaryError.GetMessage())
}