Documentation ¶
Overview ¶
Package errors provides basic utilities to construct errors.
To construct new errors or wrap other errors, use this package rather than standard libraries (errors.New, fmt.Errorf) or any other third-party libraries. This package records stack traces and chained errors, and leaves nicely formatted logs when tests fail.
Simple usage ¶
To construct a new error, use New or Errorf.
errors.New("process not found") errors.Errorf("process %d not found", pid)
To construct an error by adding context to an existing error, use Wrap or Wrapf.
errors.Wrap(err, "failed to connect to Chrome browser process") errors.Wrapf(err, "failed to connect to Chrome renderer process %d", pid)
A stack trace can be printed by passing an error to error-reporting methods in testing.State, or formatting it with the fmt package with the "%+v" verb.
Defining custom error types ¶
Sometimes you may want to define custom error types, for example, to inspect and react to errors. In that case, embed *E in your custom error struct.
type CustomError struct { *errors.E } if err := doSomething(); err != nil { return &CustomError{E: errors.Wrap(err, "something failed")} }
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func As ¶
As is a wrapper of built-in errors.As. It finds the first error in err's chain that matches target, and if so, sets target to that error value and returns true.
Types ¶
type E ¶
type E struct {
// contains filtered or unexported fields
}
E is the error implementation used by this package.
func Errorf ¶
Errorf creates a new error with the given message. This is similar to the standard fmt.Errorf, but also records the location where it was called.
func New ¶
New creates a new error with the given message. This is similar to the standard errors.New, but also records the location where it was called.
func NewE ¶
NewE create a new E. Do not use this function outside Tast. TODO: b/187792551 -- Will remove after we move all tests to use go.chromium.org/tast/errors.
func Wrap ¶
Wrap creates a new error with the given message, wrapping another error. This function also records the location where it was called. If cause is nil, this is the same as New. Note that the above behaviour is different from the popular github.com/pkg/errors package.
func Wrapf ¶
Wrapf creates a new error with the given message, wrapping another error. This function also records the location where it was called. If cause is nil, this is the same as Errorf. Note that the above behaviour is different from the popular github.com/pkg/errors package.