xerr

package
v0.0.0-...-4892080 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 26, 2024 License: GPL-3.0 Imports: 2 Imported by: 22

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

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

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

func First(errv ...error) error

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

func Merge

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

Types

type Errorv

type Errorv []error

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

func (*Errorv) Append

func (errv *Errorv) Append(err error)

Append appends err to error vector.

func (*Errorv) Appendf

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

Appendf appends formatted error string.

func (*Errorv) Appendif

func (errv *Errorv) Appendif(err error)

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

func (Errorv) Err

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

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL