go123: lab.nexedi.com/kirr/go123/xerr Index | Files

package xerr

import "lab.nexedi.com/kirr/go123/xerr"

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(&error, "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

Package Files

xerr.go

func Context Uses

func Context(errp *error, context string)

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 Uses

func Contextf(errp *error, format string, argv ...interface{})

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.

func First Uses

func First(errv ...error) error

First returns first non-nil error, or nil if there is no errors.

func Merge Uses

func Merge(errv ...error) error

Merge merges non-nil errors into one error.

it returns:

- nil                         if all errors are nil
- single error                if there is only one non-nil error
- Errorv with non-nil errors  if there is more than one non-nil error

type Errorv Uses

type Errorv []error

Errorv is error vector merging multiple errors (e.g. after collecting them from several parallel workers).

func (*Errorv) Append Uses

func (errv *Errorv) Append(err error)

Append appends err to error vector.

func (*Errorv) Appendf Uses

func (errv *Errorv) Appendf(format string, a ...interface{})

Appendf appends formatted error string.

func (*Errorv) Appendif Uses

func (errv *Errorv) Appendif(err error)

Appendif appends err to error vector if err != nil.

func (Errorv) Err Uses

func (errv Errorv) Err() error

Err returns error in canonical form accumulated in error vector.

- nil      if len(errv)==0
- errv[0]  if len(errv)==1		// XXX is this good idea?
- errv     otherwise

func (Errorv) Error Uses

func (errv Errorv) Error() string

Error returns string representation of error vector.

- ""			if len(errv)==0
- errv[0].Error()	if len(errv)==1
- "<n> errors:\n" + string representation of every error on separate line, otherwise.

Package xerr imports 2 packages (graph) and is imported by 13 packages. Updated 2019-08-28. Refresh now. Tools for package owners.