Yet another errors
package for Go.
Import as zgo.at/errors
; API docs: https://godocs.io/zgo.at/errors
This is based on the new errors API introduced in Go 1.13 with the additions:
-
Add
Wrap(err, "some context")
andWrapf(err, "some context", ...)
, which return nil if the passed error is nil.I tried using the
fmt.Errorf("...: %w", err)
pattern, but I found I missed being able to do:func f() error { return errors.Wrap(f2(), "context") }
Which I find much more convenient than:
func f() error { err := f2() if err != nil { return fmt.Errorf("context: %w", err) } return nil }
-
A stack trace is added with
erorrs.New()
,errors.Errorf()
, anderorrs.Wrap()
I know it's not needed with appropriate context but sometimes I accidentally add the same context more than once, or just want to quickly see where exactly the error is coming from. Especially on dev it's much more convenient.You can use
errors.Package
to only add stack traces for a specific package. For exampleerrors.Package = "zgo.at/goatcounter"
will only add trace lines inzgo.at/goatcounter/...
You can control the maximum stack size with
errors.StackSize
; set to0
to disable adding stack traces altogether (i.e. on production). -
Group
type for collecting grouped errors:errs := NewGroup(20) // 20 is the maximum amount of errors for { err := doStuff() if err.Append(err) { // Won't append on nil, returns false if it's nil. continue } } fmt.Println(errs) // Print all errors (if any). return errs.ErrorOrNil() // No errors? Returns nil.