Documentation ¶
Overview ¶
Package xerr provides addons for error-handling.
Error context ¶
Context and Contextf are handy to concisely add context to returned error, for example:
func myfunc(arg1, arg2 string) (..., err error) { defer xerr.Contextf(&err, "doing something (%s, %s)", arg1, arg2) ...
which will, if returned error is !nil, wrap it with the following prefix:
"doing something (%s, %s):" % (arg1, arg2)
The original unwrapped error will be still accessible as the cause of returned error. Please see package github.com/pkg/errors for details on this topic.
Error vector ¶
Sometimes there are several operations performed and we want to collect errors from them all. For this Errorv could be used which is vector of errors and at the same time an error itself. After collecting it is possible to extract resulting error from the vector in canonical form with Errorv.Err. Errorv also provides handy ways to append errors to the vector - see Errorv.Append* for details.
For convenience Merge could be used to concisely construct error vector from !nil errors and extract its canonical form in one line, for example:
err1 := op1(...) err2 := op2(...) err3 := op3(...) err := xerr.Merge(err1, err2, err3) return err
There is also First counterpart to Merge, which returns only first !nil error.
Since Errorv is actually a slice it cannot be generally compared - for example comparing 2 error interfaces that both have dynamic type Errorv will panic at runtime. However it is possible to compare Errorv to other error types, because interfaces with different dynamic types are always not equal. For example the following works:
var err error = Errorv{...} // received as result from a function if err == io.EOF { ...
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Context ¶
Context provides error context to be automatically added on error return.
Intended to be used under defer like this:
func myfunc(...) (..., err error) { defer xerr.Context(&err, "error context") ...
It is also possible to use Context directly to add context to an error if it is non-nil:
..., myerr := f() xerr.Context(&myerr, "while doing something")
which is equivalent to
import "github.com/pkg/errors" ..., myerr := f() if myerr != nil { myerr = errors.WithMessage(myerr, "while doing something") }
func Contextf ¶
Contextf provides formatted error context to be automatically added on error return.
Contextf is formatted analog of Context. Please see Context for details on how to use.
Types ¶
type Errorv ¶
type Errorv []error
Errorv is error vector merging multiple errors (e.g. after collecting them from several parallel workers).