errors

package
v0.0.0-...-678c32f Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2020 License: Apache-2.0, BSD-2-Clause Imports: 5 Imported by: 46

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

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 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 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 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