Documentation
¶
Overview ¶
Error recorder with As
recoding one code, and recording the As caller static with caller, argument information for every As func is called.
the data static format like this: ["error code", ["runtime stack of New"], ["runtime stack of As", "args of As"...], ["runtime statick of As"]...] the first one is error code, the second is New, the others are func As been called.
Example ¶
package main
import "github.com/gwaylib/errors"
func fn1(a int) error {
if a == 1 {
return errors.ErrNoData.As(a)
}
err := errors.New("not implements") // make a error code and record the first stack of caller runtime.
return err.As(a) // make the second stack of caller runtime
}
func fn2(b int) error {
return errors.As(fn1(b)) // make the third stack of caller runtime
}
func main() {
err := fn2(2)
if err != nil {
// errors.ErrNoData == err not necessarily true, so use Equal instead.
if !errors.ErrNoData.Equal(err) {
panic(err)
}
fmt.Println(err)
}
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
var (
ErrNoData = New("data not found")
)
Functions ¶
Types ¶
type ErrData ¶
type ErrData []interface{}
["error code", ["where stack of first caller ", "As args"...], ["where stack of second caller ", "As args"...]...]
type Error ¶
type Error interface {
// Return the code of make.
Code() string
// Implement the error interface of go package
Error() string
// Impelment the json marshal interface of go package.
MarshalJSON() ([]byte, error)
// Record the stack when call, and return a new error with new stack.
As(arg ...interface{}) Error
// Copy the as stack data for output
Stack() []interface{}
// Compare to another error
// It should be established with err1.Code() == err2.Code().
Equal(err error) bool
}
func As ¶
Record a stack of runtime caller and the reason with as. return a new error pointer after called. return nil if err is nil
func Parse ¶
Parse error from serial string, if it's ErrData format, create an Error of this package defined. if src is empty, return a nil Error
func ParseError ¶
Parse Error from a error instance. If the error is the type of interface Error, directly convert to the Error interface of this package. Call Parse(err.Error()) in others.