errors

package
v0.0.0-...-90deddd Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2023 License: Apache-2.0 Imports: 13 Imported by: 20

Documentation

Overview

Package errors provides a standard error definition for use in Reflow. Each error is assigned a class of error (kind) and an operation with optional arguments. Errors may be chained, and thus can be used to annotate upstream errors.

Errors may be serialized to- and deserialized from JSON, and thus shipped over network services.

Package errors provides functions Errorf and New as convenience constructors, so that users need import only one error package.

The API was inspired by package upspin.io/errors.

Index

Constants

This section is empty.

Variables

View Source
var Errorf = fmt.Errorf

Errorf is an alternate spelling of fmt.Errorf.

New is an alternate spelling of errors.New.

View Source
var Separator = ":\n\t"

Separator is inserted between chained errors while rendering. The default value (":\n\t") is intended for interactive tools. A server can set this to a different value to be more log friendly.

Functions

func E

func E(args ...interface{}) error

E is used to construct errors. E constructs errors from a set of arguments; each of which must be one of the following types:

string
	The first string argument is taken as the error's Op; subsequent
	arguments are taken as the error's Arg.
digest.Digest
	Taken as an Arg.
Kind
	Taken as the error's Kind.
error
	Taken as the error's underlying error.

If a Kind is provided, there is no further processing. If not, and an underlying error is provided, E attempts to interpret it as follows: (1) If the underlying error is another *Error, and there is no Kind argument, the Kind is inherited from the *Error. (2) If the underlying error has method Timeout() bool, it is invoked, and if it returns true, the error's kind is set to Timeout. (3) If the underlying error has method Temporary() bool, it is invoked, and if it returns true, the error's kind is set to Temporary. (4) If the underyling error is context.Canceled, the error's kind is set to Canceled. (5) If the underlying error is an os.IsNotExist error, the error's kind is set to NotExist.

func Is

func Is(kind Kind, err error) bool

Is tells whether an error has a specified kind, except for the indeterminate kind Other. In the case an error has kind Other, the chain is traversed until a non-Other error is encountered.

func Match

func Match(err1, err2 error) bool

Match compares err1 with err2. If err1 has type Kind, Match reports whether err2's Kind is the same, otherwise, Match checks that every nonempty field in err1 has the same value in err2. If err1 is an *Error with a non-nil Err field, Match recurs to check that the two errors chain of underlying errors also match.

func NonRetryable

func NonRetryable(err error) bool

NonRetryable tells whether error err is likely fatal, and thus not useful to retry. The passed in error must not be nil. Note that NonRetryable != !Restartable; these functions are not the inverse of each other. They each report on the errors they know to be either non-retryable or restartable, but there exist errors which don't belong to either category.

func Restartable

func Restartable(err error) bool

Restartable determines if the provided error (must be non-nil) is restartable. Restartable errors include transient errors and network errors. Restartable traverses root causes and checks if any of the underlying causes are transient or network errors. Additionally, when traversing causes, any "github.com/grailbio/base/errors" error is recovered into "github.com/grailbio/reflow/errors" with its kind derived from base kind and severity.

func Transient

func Transient(err error) bool

Transient tells whether error err is likely transient, and thus may be usefully retried. The passed in error must not be nil.

func WithRetryableKinds

func WithRetryableKinds(ctx context.Context, ks ...Kind) context.Context

WithRetryableKinds returns a child context of ctx with the given error kinds and any pre-existing ones. WithRetryableKinds should be used rather than directly setting the value to avoid overwriting any previously set retryable errors.

Types

type Error

type Error struct {
	// Kind is the error's type.
	Kind Kind
	// Op is a one-word description of the operation that errored.
	Op string
	// Arg is an (optional) list of arguments to the operation.
	Arg []string
	// Err is this error's underlying error: this error is caused
	// by Err.
	Err error
}

Error defines a Reflow error. It is used to indicate an error associated with an operation (and arguments), and may wrap another error.

Errors should be constructed by errors.E.

func Recover

func Recover(err error) *Error

Recover recovers any error into an *Error. If the passed-in err is already an *Error or nil, it is simply returned; otherwise it is wrapped.

func RecoverError

func RecoverError(err error) (*Error, bool)

RecoverError recovers a *Error from the given error, and if is of type: - *Error, it is returned as-is. - *baseerrors.Error, a *Error is returned with a translated Kind. RecoverError returns whether the given error was successfully recovered.

func (*Error) Copy

func (e *Error) Copy() *Error

Copy creates a shallow copy of Error e.

func (*Error) Error

func (e *Error) Error() string

Error renders this error and its chain of underlying errors, separated by Separator.

func (*Error) ErrorSeparator

func (e *Error) ErrorSeparator(sep string) string

ErrorSeparator renders this errors and its chain of underlying errors, separated by sep.

func (*Error) HTTPStatus

func (e *Error) HTTPStatus() int

HTTPStatus indicates the HTTP status that should be presented in conjunction with this error.

func (*Error) MarshalJSON

func (e *Error) MarshalJSON() ([]byte, error)

MarshalJSON implements JSON marshalling for Error.

func (*Error) Temporary

func (e *Error) Temporary() bool

Temporary tells whether this error is temporary.

func (*Error) Timeout

func (e *Error) Timeout() bool

Timeout tells whether this error is a timeout error.

func (*Error) UnmarshalJSON

func (e *Error) UnmarshalJSON(b []byte) error

UnmarshalJSON implements JSON unmarshalling for Error.

type Kind

type Kind int

Kind denotes the type of the error. The error's kind is used to render the error message and also for interpretation.

const (
	// Other denotes an unknown error.
	Other Kind = iota
	// Canceled denotes a cancellation error.
	Canceled
	// Timeout denotes a timeout error.
	Timeout
	// Temporary denotes a transient error.
	Temporary
	// NotExist denotes an error originating from a nonexistant resource.
	NotExist
	// NotAllowed denotes a permissions error.
	NotAllowed
	// NotSupported indicates the operation was not supported.
	NotSupported
	// TooManyTries indicates that the operation was retried too many times.
	TooManyTries
	// Integrity denotes an integrity check error.
	Integrity
	// ResourcesExhausted indicates that there were insufficient resources.
	ResourcesExhausted
	// Eval denotes an evaluation error.
	Eval
	// Unavailable denotes that a resource is temporarily unavailable.
	Unavailable
	// Fatal denotes an unrecoverable error.
	Fatal
	// Invalid indicates an invalid state or data.
	Invalid
	// Net indicates a networking error.
	Net
	// Precondition indicates that a precondition was not met.
	Precondition
	// OOM indicates a out-of-memory error.
	OOM
	// Module indicates a reflow module error.
	Module
	// DockerExec indicates a reflow exec error.
	DockerExec
)

func BaseToReflow

func BaseToReflow(baseKind baseerrors.Kind, baseSeverity baseerrors.Severity) Kind

BaseToReflow interprets base error Kind and Severity to reflow error Kind.

func GetRetryableKinds

func GetRetryableKinds(ctx context.Context) []Kind

GetRetryableKinds returns a slice of any retryable error kinds stored in ctx.

func (Kind) String

func (k Kind) String() string

String renders a human-readable description of kind k.

type Multi

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

Multi represents a composite error which combines multiple (non-nil) errors. Multi is useful in cases where there is a need to (concurrently) combine multiple (non-critical) errors into one.

func (*Multi) Add

func (e *Multi) Add(errs ...error)

Add adds the given set of errors (ignoring nil errors). Add can be called concurrently.

func (*Multi) Combined

func (e *Multi) Combined() error

Combined returns an error which combines all the underlying errors.

Jump to

Keyboard shortcuts

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