errors

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2019 License: MIT Imports: 7 Imported by: 51

README

errors

The substitute for the golang pkg/errors, support print call stack and more ..

Wrap exists error object

errors library support wrap exists error object to support stack trace

func TestExample(t *testing.T) {
	err := fmt.Errorf("test")
	err = Wrap(err, "create new stack trace wrapper error")

	// look through callstack
	// StackTrace(err, func(frame runtime.Frame) {
	// 	println(fmt.Sprintf("%s(%s:%d)\n", frame.Function, filepath.Base(frame.File), frame.Line))
	// })

	println(err.Error())
}

above codes will print like this:

error: create new stack trace wrapper error
    at github.com/BlockchainMiddleware/errors.newCallStackError(errors.go:21)
    at github.com/BlockchainMiddleware/errors.Wrap(errors.go:81)
    at github.com/BlockchainMiddleware/errors.TestExample(errors_test.go:86)
    at testing.tRunner(testing.go:865)
    at runtime.goexit(asm_amd64.s:1337)
cause by test

Unwrap error object

errors support unwrap error object to get original cause error

err := fmt.Errorf("test")
err1 := errors.Wrap(err,"layer 1")
err2 := errors.Wrap(err,"layer 2")

// require.Equal(t,errors.Unwrap(err2),err)

Enhance error interface

errors support create error object with vendor and error code attributes.

ec := errors.New("test",errors.WithCode(-2),errors.WithVendor("test"))

vendor,ok := errors.Vendor(ec)
// require.True(t,ok)
// require.Equal(t,vendor,"test")

code,ok := errors.Code(ec)
// require.True(t,ok)
// require.Equal(t,code,-2)

JSON support

errors support marshal to json/ unmarshal from json

unmarshal from json like this:

	// if unmarshal failed this function return nil
	ec := errors.FromJSON(data)

one more thing: errors also support bind customer attributes. for details lookup test example

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Debug = true

Debug print stack information flag

View Source
var ErrTODO = New("todo")

ErrTODO errors

Functions

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.

The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.

An error matches target if the error's concrete value is assignable to the value pointed to by target, or if the error has a method As(interface{}) bool such that As(target) returns true. In the latter case, the As method is responsible for setting target.

As will panic if target is not a non-nil pointer to either a type that implements error, or to any interface type. As returns false if err is nil.

func Attr

func Attr(err error, name string, value interface{}) bool

Attr get associate attribute value

func Cause

func Cause(err error) error

Cause get error's cause error

func Code

func Code(err error) (int, bool)

Code get error associate code

func FromJSON added in v0.0.3

func FromJSON(data []byte) error

FromJSON unmarshal errorcode from json if any error occur, return nil

func Is

func Is(first error, second error) bool

Is Is unwraps its first argument sequentially looking for an error that matches the second. It reports whether it finds a match. It should be used in preference to simple equality checks:

func New

func New(errmsg string, options ...Option) error

New create errors enhance error object which support errorcode and vendor id

func StackTrace

func StackTrace(err error, tracer func(runtime.Frame))

StackTrace error raise stack trace function

func TODO

func TODO(message string) error

TODO invoke todo panic

func Unwrap

func Unwrap(err error) error

Unwrap walk throught the error cause list and return the header cause error

func Vendor

func Vendor(err error) (string, bool)

Vendor get error associate vendor name

func Wrap

func Wrap(err error, fmtstr string, args ...interface{}) error

Wrap wrap error with stacktrace

Types

type ErrorCode added in v0.0.3

type ErrorCode struct {
	Vendor  string                 `json:"vendor"`
	Code    int                    `json:"code"`
	Message string                 `json:"message"`
	Attrs   map[string]interface{} `json:"attrs"`
}

ErrorCode .

func (*ErrorCode) Error added in v0.0.3

func (ec *ErrorCode) Error() string

type Option

type Option func(ec *ErrorCode)

Option .

func WithAttr

func WithAttr(name string, value interface{}) Option

WithAttr bind error customer attribute

func WithCode

func WithCode(code int) Option

WithCode error code option

func WithVendor

func WithVendor(vendor string) Option

WithVendor error vendor option

Jump to

Keyboard shortcuts

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