errors

package module
v0.0.0-...-780b2e8 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2020 License: BSD-3-Clause Imports: 2 Imported by: 2

Documentation

Overview

Package errors extends the standard library's functions to manipulate errors.

The New type is drop in compatible with the standard library function and creates errors whose only content is a text message. It also allows for the creation of constant errors:

const ErrSentinel = errors.New("...")

The Unwrap, Is and As functions work on errors that may wrap other errors. An error wraps another error if its type has the method

Unwrap() error

If e.Unwrap() returns a non-nil error w, then we say that e wraps w.

Unwrap unpacks wrapped errors. If its argument's type has an Unwrap method, it calls the method once. Otherwise, it returns nil.

A simple way to create wrapped errors is to call fmt.Errorf and apply the %w verb to the error argument:

errors.Unwrap(fmt.Errorf("... %w ...", ..., err, ...))

returns err.

Is unwraps its first argument sequentially looking for an error that matches the second. It reports whether it finds a match. It should be used in preference to simple equality checks:

if errors.Is(err, os.ErrExist)

is preferable to

if err == os.ErrExist

because the former will succeed if err wraps os.ErrExist.

As unwraps its first argument sequentially looking for an error that can be assigned to its second argument, which must be a pointer. If it succeeds, it performs the assignment and returns true. Otherwise, it returns false. The form

var perr *os.PathError
if errors.As(err, &perr) {
 fmt.Println(perr.Path)
}

is preferable to

if perr, ok := err.(*os.PathError); ok {
 fmt.Println(perr.Path)
}

because the former will succeed if err wraps an *os.PathError.

Check confirms the status an error and returns early using the context provided by Handlef. It can be useful in removing error handling boiler plate. Returned values are implicitly checked.

    func Set(v interface{}) (err error) {
     defer errors.Handlef(&err, "setting: %v", v)
     resp, err := set(v)
     errors.Check(err)
	return resp.Close()
    }

BUG: Handlef is unable to distinguish panic(nil) from not panicing, thus they will be swallowed. This is a known limiation of panic/recover.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target interface{}) bool

As finds the first error in err's chain that matches target, and if so, sets target to that error value and returns true.

The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.

An error matches target if the error's concrete value is assignable to the value pointed to by target, or if the error has a method As(interface{}) bool such that As(target) returns true. In the latter case, the As method is responsible for setting target.

As will panic if target is not a non-nil pointer to either a type that implements error, or to any interface type. As returns false if err is nil.

func Check

func Check(err error)

Check validates if an error is nil.

It must be used with Handlef, otherwise your code will panic.

func Handlef

func Handlef(err *error, format string, args ...interface{})

Handlef wraps non-nil Checked and returned errors.

It should only be defered, and must come before any calls to Check. Returned errors are also wrapped to prevent mistakes and support direct returns.

func Is

func Is(err, target error) bool

Is reports whether any error in err's chain matches target.

The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.

An error is considered to match a target if it is equal to that target or if it implements a method Is(error) bool such that Is(target) returns true.

func Unwrap

func Unwrap(err error) error

Unwrap returns the result of calling the Unwrap method on err, if err's type contains an Unwrap method returning error. Otherwise, Unwrap returns nil.

Types

type New

type New string

New is an error that holds the given string.

Unlike the standard library, if the text is identical instances are not distinct error values.

func (New) Error

func (n New) Error() string

Error concerts a New into a string and implements the error interface.

Jump to

Keyboard shortcuts

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