errors

package
v0.0.0-...-d9bdf42 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: Unlicense Imports: 5 Imported by: 1

Documentation

Overview

Package errors implements functions to manipulate errors.

The New function creates errors whose contents consist of a textual description of the error and contextual information. This contextual information can be used to trace back the source of the error. The Trace function can be used to write traceback information to an io.Writer in a human-friendly format.

Errors are frequently caused by other errors. To account for this, the New function takes two parameters: a textual description of the error and its parent error.

Package developers may choose to predefine certain errors, such as the io.EOF error:

var EOF = errors.New("EOF")

However, the context of this error is the line on which it is declared. To update the contextual information, the Raise function can be invoked when intending to raise a predefined error from a current context, like so:

errors.Raise(EOF)

A function which handles errors from multiple concurrently executing processes should, in most cases, return the first error it receives:

func f() error {
	errs := make(chan error)
	for i := 0; i < 5; i++ {
		go g(errs)
	}
	for err := range errs {
		if err != nil {
			return err
		}
	}
	return nil
}

Alternatively, if the developer's intent is to return all received errors, the Join function is provided to return all errors as one. However, this removes the context behind the combined errors.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Has

func Has(err, target error) bool

Has reports whether the textual error description of err or any of its parent errors match the textual error description of target.

func Is

func Is(err, target error) bool

Is reports whether the textual error description of err matches the textual error description of target.

func Join

func Join(errs ...error) error

Join returns an error that combines the given errs. Any nil error values are discarded. Join returns nil if errs contains no non-nil values. The resultant error is formatted as a concatenation of the textual error descriptions of all given errs, with a comma and space between each description.

An error can only have one parent, so the resultant error has nil parent.

func New

func New(text string, err error) error

New returns an error whose textual error description is given by text, and whose parent error is err. If the new error has no parent, err should be given as nil.

The current filename, line, program counter, and parent function name are stored within the error interface. Each call to New returns a distinct error value even if text is identical.

func Raise

func Raise(err error) error

func Trace

func Trace(w io.Writer, err error)

Trace writes human-friendly error traceback information from err to w. If w is nil, Trace writes to the standard error stream.

Types

type Error

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

Error represents an error. The Error type holds a textual description of the error and contextual information describing the source of the error. It also holds a copy of its parent error, if one exists.

Contextual information held in an Error can be used to trace back the source of an error, and can be returned to the user through functions such as errors.Trace

func (Error) Addr

func (e Error) Addr() uintptr

func (Error) Error

func (e Error) Error() string

func (Error) File

func (e Error) File() string

func (Error) Func

func (e Error) Func() string

func (Error) Line

func (e Error) Line() int

func (Error) Parent

func (e Error) Parent() error

func (Error) Text

func (e Error) Text() string

Jump to

Keyboard shortcuts

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