Documentation ¶
Overview ¶
错误记录器 error recorder
本设计补充并实现了系统的error接口。 This design complements and implements the error interface of the go package.
用于发生错误时附带发生错误的原因、位置等信息以便还原现场。 It can be used to restore the scene with the information of the cause and location of the error when it occurs.
因本错误设计含有比较大的数据量信息,因此需要注意被调用的频率,以避免影响到系统效率。 Because this incorrect design contains a large amount of data information, we need to pay attention to the frequency of calls to avoid affecting system efficiency.
使用例子 Example
package main
import "github.com/gwaylib/errors"
func fn1(a int) error { if a == 1{ return errors.ErrNoData.As(a) } return errors.New("not implements").As(a) } func fn2(b int) error { return errors.As(fn1(b)) } 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) } // Deals the same error. fmt.Println(err) } }
Index ¶
Constants ¶
This section is empty.
Variables ¶
var (
ErrNoData = New("data not found")
)
Functions ¶
Types ¶
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 // Compare to another error // It should be established with err1.Code() == err2.Code(). Equal(err error) bool }
func As ¶
Record the reason with as, and return a new error with new stack of reason. It would be safe for concurrency.
func ParseError ¶
Parse Error from a error instance. If the error is type of Error, it will be return directly.