https://github.com/src-d/go-errors
Yet another errors package, implementing error handling primitives.
https://github.com/src-d/go-errors
error-handling golang stacktrace
Last synced: 4 months ago
JSON representation
Yet another errors package, implementing error handling primitives.
- Host: GitHub
- URL: https://github.com/src-d/go-errors
- Owner: src-d
- License: apache-2.0
- Created: 2016-11-28T18:58:11.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2019-09-30T12:56:36.000Z (about 6 years ago)
- Last Synced: 2025-05-05T05:05:43.565Z (6 months ago)
- Topics: error-handling, golang, stacktrace
- Language: Go
- Homepage:
- Size: 18.6 KB
- Stars: 23
- Watchers: 6
- Forks: 8
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-errors [](https://godoc.org/gopkg.in/src-d/go-errors.v1) [](https://travis-ci.org/src-d/go-errors) [](https://codecov.io/gh/src-d/go-errors) [](https://codebeat.co/projects/github-com-src-d-go-errors)
Yet another `errors` package, implementing error handling primitives with error wrapping and error tracing.
## Installation
The recommended way to install go-errors is:
```
go get -u gopkg.in/src-d/go-errors.v1
```
## Examples
The `Kind` type allows you to create new errors containing the stack trace and also check if an error is of a particular kind.
```go
var ErrExample = errors.NewKind("example")
err := ErrExample.New()
if ErrExample.Is(err) {
fmt.Printf("%+v\n", err)
}
// Example Output:
// example
//
// gopkg.in/src-d/errors%2v0_test.ExampleError_Format
// /home/mcuadros/workspace/go/src/gopkg.in/src-d/errors.v0/example_test.go:60
// testing.runExample
// /usr/lib/go/src/testing/example.go:114
// testing.RunExamples
// /usr/lib/go/src/testing/example.go:38
// testing.(*M).Run
// /usr/lib/go/src/testing/testing.go:744
// main.main
// github.com/pkg/errors/_test/_testmain.go:106
// runtime.main
// /usr/lib/go/src/runtime/proc.go:183
// runtime.goexit
// /usr/lib/go/src/runtime/asm_amd64.s:2086
```
### Error with format
```go
var ErrMaxLimitReached = errors.NewKind("max. limit reached: %d")
err := ErrMaxLimitReached.New(42)
if ErrMaxLimitReached.Is(err) {
fmt.Println(err)
}
// Output: max. limit reached: 42
```
### Error wrapping
```go
var ErrNetworking = errors.NewKind("network error")
err := ErrNetworking.Wrap(io.EOF)
if ErrNetworking.Is(err) {
fmt.Println(err)
}
// Output: network error: EOF
```
You can find these examples and many more in the [examples](example_test.go) file.
## License
Apache License 2.0, see [LICENSE](LICENSE)