This project adheres to Semantic Versioning. This package is considered mature, you should expect package stability in Minor and Patch version releases
- Major: backwards incompatible package updates
- Minor: feature additions, removal of deprecated features
- Patch: bug fixes, backward compatible model and function changes, etc.
errors
provides simple, concise, useful error handling and annotation. This package aims to implement the Error Inspection and Error Values Go2 draft designs.
One of the biggest frustrations with Go error handling is the lack of forensic and meta information errors should provide. By default errors are just a string and possibly a type. They can't tell you where they occurred or the path through the call stack they followed. The error implementation in Go is robust enough to control program flow but it's not very efficient for troubleshooting or analysis.
Since the idom in Go is that we pass the error back up the stack anyway:
if nil != err {
return err
}
it's trivial to make errors much more informative with a simple error package. bdlm/errors
makes this easy and supports tracing the call stack and the error callers with relative ease. Custom error types are also fully compatible with this package and can be used freely.
go get github.com/bdlm/errors/v2
See the documentation for more examples. All package methods work with any error
type as well as nil
values, and error instances implement the Unwrap
, Is
, Marshaler
, and Formatter
interfaces as well as the github.com/bdlm/std/errors
interfaces.
var MyError = errors.New("My error")
var MyError = errors.Errorf("My error #%d", 1)
if nil != err {
return errors.Wrap(err, "the operation failed")
}
err := try1()
if nil != err {
err2 := try2()
if nil != err2 {
return errors.WrapE(err, err2)
}
return err
}
err := doWork()
if prevErr := errors.Unwrap(err); nil != prevErr {
...
}
var MyError = errors.New("My error")
func main() {
err := doWork()
if errors.Is(err, MyError) {
...
}
}
err := doWork()
for nil != err {
fmt.Println(err)
err = errors.Unwrap(err)
}
errors
implements the %s
and %v
fmt.Formatter
formatting verbs and several modifier flags:
%s
- Returns the error string of the last error added.%v
- Alias for%s
#
- JSON formatted output, useful for logging-
- Output caller details, useful for troubleshooting
fmt.Printf("%s", err)
An error occurred
fmt.Printf("%v", err)
An error occurred
fmt.Printf("%-v", err)
#0 stack_test.go:40 (github.com/bdlm/error_test.TestErrors) - An error occurred
fmt.Printf("% v", err)
#0 stack_test.go:40 (github.com/bdlm/error_test.TestErrors) - An error occurred #1 stack_test.go:39 (github.com/bdlm/error_test.TestErrors) - An error occurred
fmt.Printf("%#v", err)
{"error":"An error occurred"}
fmt.Printf("%#-v", err)
{"caller":"#0 stack_test.go:40 (github.com/bdlm/error_test.TestErrors)","error":"An error occurred"}
fmt.Printf("%# v", err)
[{"caller":"#0 stack_test.go:40 (github.com/bdlm/error_test.TestErrors)","error":"An error occurred"},{"caller":"#1 stack_test.go:39 (github.com/bdlm/error_test.TestErrors)","error":"An error occurred"}]
fmt.Printf("% #-v", err)
{
"caller": "#0 stack_test.go:40 (github.com/bdlm/error_test.TestErrors)",
"error": "An error occurred"
}
fmt.Printf("% # v", err)
[
{
"caller": "#0 stack_test.go:40 (github.com/bdlm/error_test.TestErrors)",
"error": "An error occurred"
},
{
"caller": "#1 stack_test.go:39 (github.com/bdlm/error_test.TestErrors)",
"error": "An error occurred"
}
]
See the documentation for more examples.