https://github.com/teamwork/guru
Go errors with a Guru Meditation Number
https://github.com/teamwork/guru
go
Last synced: about 1 year ago
JSON representation
Go errors with a Guru Meditation Number
- Host: GitHub
- URL: https://github.com/teamwork/guru
- Owner: Teamwork
- License: mit
- Created: 2017-07-04T14:55:25.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2024-10-21T12:04:56.000Z (over 1 year ago)
- Last Synced: 2025-04-05T03:12:44.692Z (about 1 year ago)
- Topics: go
- Language: Go
- Homepage:
- Size: 57.6 KB
- Stars: 8
- Watchers: 48
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.com/Teamwork/guru)
[](https://codecov.io/gh/Teamwork/guru)
[](http://godoc.org/github.com/Teamwork/guru)
The guru package allows adding a Guru Meditation Number to errors:
```go
// Error constants.
const (
CodeFruitOverflow = iota + 1
CodeBoozeUnderrun
CodeExpired
)
func Example() {
// Construct a new error.
err := guru.New(CodeFruitOverflow, "too many bananas")
fmt.Println(err) // error 1: too many bananas
// Retrieve the error code
code := guru.Code(err)
fmt.Println(code) // 1
// Add error code to existing error.
err = errors.New("not enough beer")
err = guru.WithCode(CodeBoozeUnderrun, err)
fmt.Println(err) // error 2: not enough beer
// Add error code to existing error with context.
err = errors.New("Dennis Ritchie")
err = guru.Wrap(CodeExpired, err, "no longer with us")
fmt.Println(err) // error 3: Dennis Ritchie: no longer with us
// For HTTP applications, it may be useful to directly the HTTP status codes:
err = guru.New(http.StatusNotAcceptable, "Justin Bieber")
fmt.Println(err) // error 406: Justin Bieber
// Error codes can be overriden:
err = guru.New(1, "oh noes")
err = guru.WithCode(2, err)
fmt.Println(guru.Code(err)) // 2
}
```
guru is built on top of [github.com/pkg/errors](https://github.com/pkg/errors);
all errors that guru returns are github.com/pkg/errors.