errors

package
v0.0.0-...-15a9a0c Latest Latest
Warning

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

Go to latest
Published: May 19, 2023 License: BSD-3-Clause Imports: 5 Imported by: 0

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

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

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.

func Is

func Is(err, target error) bool

Is is a wrapper of built-in errors.Is. It reports whether any error in err's chain matches target.

func Join

func Join(errs ...error) error

Join is a wrapper of built-in errors.Join.

func Unwrap

func Unwrap(err error) error

Unwrap is a wrapper of built-in errors.Unwrap. It 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 E

type E struct {
	// contains filtered or unexported fields
}

E is the error implementation used by this package.

func Errorf

func Errorf(format string, args ...interface{}) *E

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

func New(msg string) *E

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

func NewE(msg string, stk stack.Stack, cause error) *E

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

func Wrap(cause error, msg string) *E

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

func Wrapf(cause error, format string, args ...interface{}) *E

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.

func (*E) Error

func (e *E) Error() string

Error implements the error interface.

func (*E) Format

func (e *E) Format(s fmt.State, verb rune)

Format implements the fmt.Formatter interface. In particular, it is supported to format an error chain by "%+v" verb.

func (*E) Unwrap

func (e *E) Unwrap() error

Unwrap implements the error Unwrap interface introduced in go1.13.

Directories

Path Synopsis
Package stack provides a utility to capture and format a stack trace.
Package stack provides a utility to capture and format a stack trace.

Jump to

Keyboard shortcuts

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