errors

package
v0.0.0-...-38575d5 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: AGPL-3.0, BSD-2-Clause Imports: 6 Imported by: 13

README

errors Travis-CI AppVeyor GoDoc Report card

Package errors provides simple error handling primitives.

The traditional error handling idiom in Go is roughly akin to

if err != nil {
        return err
}

which applied recursively up the call stack results in error reports without context or debugging information. The errors package allows programmers to add context to the failure path in their code in a way that does not destroy the original value of the error.

Adding context to an error

The errors.Wrap function returns a new error that adds context to the original error. For example

_, err := ioutil.ReadAll(r)
if err != nil {
        return errors.Wrap(err, "read failed")
}

Retrieving the cause of an error

Using errors.Wrap constructs a stack of errors, adding context to the preceding error. Depending on the nature of the error it may be necessary to reverse the operation of errors.Wrap to retrieve the original error for inspection. Any error value which implements this interface can be inspected by errors.Cause.

type causer interface {
        Cause() error
}

errors.Cause will recursively retrieve the topmost error which does not implement causer, which is assumed to be the original cause. For example:

switch err := errors.Cause(err).(type) {
case *MyError:
        // handle specifically
default:
        // unknown error
}

Read the package documentation for more information.

Contributing

We welcome pull requests, bug fixes and issue reports. With that said, the bar for adding new symbols to this package is intentionally set high.

Before proposing a change, please discuss your change by raising an issue.

Licence

BSD-2-Clause

Documentation

Overview

multierror is a simple Go package for combining multiple errors together.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cause

func Cause(err error) error

Cause returns the underlying cause of the error, if possible. An error value has a cause if it implements the following interface:

type Causer interface {
       Cause() error
}

If the error does not implement Cause, the original error will be returned. If the error is nil, nil will be returned without further investigation.

func Error

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

func Errorf

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

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

func New

func New(message string) error

New returns an error with the supplied message.

func NewWithCode

func NewWithCode(err error, code ErrorCode, msg string) error

NewWithCode create a error with error code and message

func NewWithPayload

func NewWithPayload(err error, code ErrorCode, payload interface{}, msg string) error

NewWithPayload create error with error code and payload and message

func Wrap

func Wrap(err error, message string) error

Wrap returns an error annotating err with message. If err is nil, Wrap returns nil.

func Wrapf

func Wrapf(err error, format string, args ...interface{}) error

Wrapf returns an error annotating err with the format specifier. If err is nil, Wrapf returns nil.

Types

type ErrorCode

type ErrorCode int

ErrorCode is enum object of errors

var (
	// Default unknow error
	Default          ErrorCode = 1
	InvalidParameter ErrorCode = 2
	// JSONIsEmpty error when json is empty
	JSONIsEmpty ErrorCode = 100
	// BodyEmpty error when body is empty
	BodyEmpty ErrorCode = 101
	// URLRedirected error when url redirected
	URLRedirected ErrorCode = 102
)

func Code

func Code(err error) ErrorCode

Code return error code

func CodeWithPayload

func CodeWithPayload(err error) (ErrorCode, interface{})

CodeWithPayload return error code and payload

type ErrorPayload

type ErrorPayload interface{}

ErrorPayload is detail payload of errors

type Errors

type Errors []error

The Errors type wraps a slice of errors

func (Errors) Err

func (e Errors) Err() error

Returns a MultiError struct containing this Errors instance, or nil if there are zero errors contained.

type Frame

type Frame uintptr

Frame represents a program counter inside a stack frame.

func (Frame) Format

func (f Frame) Format(s fmt.State, verb rune)

Format formats the frame according to the fmt.Formatter interface.

%s    source file
%d    source line
%n    function name
%v    equivalent to %s:%d

Format accepts flags that alter the printing of some verbs, as follows:

%+s   path of source file relative to the compile time GOPATH
%+v   equivalent to %+s:%d

type MultiError

type MultiError struct {
	Errors Errors
}

The MultiError type implements the error interface, and contains the Errors used to construct it.

func (*MultiError) Error

func (m *MultiError) Error() string

Returns a concatenated string of the contained errors

type StackTrace

type StackTrace []Frame

StackTrace is stack of Frames from innermost (newest) to outermost (oldest).

func (StackTrace) Format

func (st StackTrace) Format(s fmt.State, verb rune)

Format format stack trace

Jump to

Keyboard shortcuts

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