# 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
# Functions
As finds the first error in err's chain that matches target, and if so, sets target to that error value and returns true.
Attr get associate attribute value.
Cause get error's cause error.
Code get error associate code.
FromJSON unmarshal errorcode from json if any error occur, return nil.
Is Is unwraps its first argument sequentially looking for an error that matches the second.
New create errors enhance error object which support errorcode and vendor id.
StackTrace error raise stack trace function.
TODO invoke todo panic.
Unwrap walk throught the error cause list and return the header cause error.
Vendor get error associate vendor name.
WithAttr bind error customer attribute.
WithCode error code option.
WithVendor error vendor option.
Wrap wrap error with stacktrace.
# Type aliases
Option .