基本介绍

GoFrame框架提供了强大、丰富的错误处理能力,由gerror组件实现。
使用方式

  1. import "github.com/gogf/gf/errors/gerror"

接口文档
https://godoc.org/github.com/gogf/gf/errors/gerror

知识图谱

GoFrame Error Handling.png

错误创建

New/Newf

  1. func New(text string) error
  2. func Newf(format string, args ...interface{}) error

用于创建一个自定义错误信息的error对象,并包含堆栈信息。

Wrap/Wrapf

  1. func Wrap(err error, text string) error
  2. func Wrapf(err error, format string, args ...interface{}) error

用于包裹其他错误error对象,构造成多级的错误信息,包含堆栈信息。

NewSkip/NewSkipf

  1. func NewSkip(skip int, text string) error
  2. func NewSkipf(skip int, format string, args ...interface{}) error

用于创建一个自定义错误信息的error对象,并且忽略部分堆栈信息(按照当前调用方法位置往上忽略)。高级功能,一般开发者很少用得到。

错误码相关方法

  1. func NewCode(code int, text string) error
  2. func NewCodef(code int, format string, args ...interface{}) error
  3. func NewCodeSkip(code, skip int, text string) error
  4. func NewCodeSkipf(code, skip int, format string, args ...interface{}) error
  5. func WrapCode(code int, err error, text string) error
  6. func WrapCodef(code int, err error, format string, args ...interface{}) error

具体介绍请参考后续 错误处理-错误码特性 章节。

基准测试

常用方法的基准性能测试:https://github.com/gogf/gf/blob/master/errors/gerror/gerror_z_bench_test.go