errors

package
v0.0.0-...-e1e9d1d Latest Latest
Warning

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

Go to latest
Published: May 20, 2021 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package errors provides functions and types to define and manipulate errors. It should generally be used instead of the standard library's errors package, as it is a strict superset.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Arg

func Arg(e error, name string) error

Arg returns an error that wraps e indicating an argument error for the parameter of the specified name.

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.

func Errorf

func Errorf(f string, args ...interface{}) error

Errorf formats according to a format specifier and returns the string as a value that satisfies error.

If the format specifier includes a %w verb with an error operand, the returned error will implement an Unwrap method returning the operand. It is invalid to include more than one %w verb or to supply it with an operand that does not implement the error interface. The %w verb is otherwise a synonym for %v.

func Is

func Is(err, target error) bool

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

func IsArg

func IsArg(e error, names ...string) bool

IsArg returns true if e or any error in its chain is an ArgError marked with any of the provided parameter names, otherwise it returns false.

func IsStatus

func IsStatus(e error, codes ...int) bool

IsStatus returns true if e or any error in its chain is marked with any of the provided status codes, otherwise it returns false.

func IsTag

func IsTag(e error, tags ...string) bool

IsTag returns true if e or any error in its chain is tagged by any of the provided tags, otherwise it returns false.

func New

func New(s string) error

New returns an error with s as error message.

func Status

func Status(e error, code int) error

Status returns an error that wraps e and marks it with the provided HTTP status code. Errors can be queried for status with IsStatus. If e is nil, it returns nil.

func Tag

func Tag(e error, tag string) error

Tag returns an error that wraps e and tags it with the provided arbitrary tag. Errors can be queried for tags with IsTag. If e is nil, it returns nil.

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 ArgError

type ArgError struct {
	// Name is the parameter name of the invalid argument.
	Name string
	// Err is the error wrapped by this ArgError.
	Err error
}

ArgError is an error due to an invalid argument.

func (ArgError) Error

func (e ArgError) Error() string

Error returns the error message.

func (ArgError) Unwrap

func (e ArgError) Unwrap() error

Unwrap returns the error wrapped by ArgError.

type ConstError

type ConstError string

ConstError is an error string that can be defined as constant.

const (
	// ErrNotMocked is returned when a method is called on a mock but no
	// mocked implementation was set up.
	ErrNotMocked ConstError = "not mocked"
)

func (ConstError) Error

func (e ConstError) Error() string

Error returns the error message of the ConstError, which is the constant string value itself.

type StatusError

type StatusError struct {
	// Code is the status code associated with the error. By convention,
	// it should be an HTTP status code (typically in the 4xx-5xx range).
	Code int

	// Err is the error wrapped by this StatusError.
	Err error
}

StatusError is an error marked with a status code, which by convention should be an HTTP status code indicating the type of error it wraps. E.g. 404 if a database row was not found, 409 for a unique constraint violation, 500 for a connection error, etc.

func (StatusError) Error

func (e StatusError) Error() string

Error returns the error message.

func (StatusError) Unwrap

func (e StatusError) Unwrap() error

Unwrap returns the error wrapped by StatusError.

Jump to

Keyboard shortcuts

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