NewOption自定义的错误创建

  • 说明:用于自定义配置的错误对象创建。
  • 示例:

    1. func ExampleNewOption() {
    2. err := gerror.NewOption(gerror.Option{
    3. Text: "this feature is disabled in this storage",
    4. Code: gcode.CodeNotSupported,
    5. })
    6. }

fmt 格式化

使用示例:

日志输出支持

使用示例:

  1. package main
  2. "errors"
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/errors/gerror"
  5. )
  6. func main() {
  7. var err error
  8. err = errors.New("sql error")
  9. err = gerror.Wrap(err, "adding failed")
  10. err = gerror.Wrap(err, "api calling failed")
  11. g.Log().Printf("%+v", err)
  12. }
  13. // Output:
  14. // 1. api calling failed
  15. // 1). main.main
  16. // /Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/other/test.go:14
  17. // 2. adding failed
  18. // 1). main.main
  19. // /Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/other/test.go:13
  20. // 3. sql error

简化堆栈输出

从框架v2.2.0版本开始,我们可以通过环境变量配置GF_GERROR_BRIEF=true或者命令行启动参数--gf.gerror.brief=true来启用错误堆栈的简略模式,在简略模式下,堆栈信息中将不会打印框架的堆栈信息。以上堆栈信息将会变为:

  1. 2022-10-08 21:07:00.751 [ERRO] {328d1204e2191c179a09086890c857b8} request done, cost: 3 ms, code: -1, message: "", detail: <nil>, error: GetParams failed: {ResourceId:tdxxxx-a2c378bd Component: Version:0}: rpc error: code = NotFound desc = cluster.khaos.tencent.com "tdxxxx-a2c378bd" not found
  2. 1. GetParams failed: {ResourceId:tdxxxx-a2c378bd Component: Version:0}
  3. 1). git.woa.com/khaos/eros/app/khaos-oss/internal/logic/params.(*sParams).doGetParamsJson
  4. /root/workspace/khaos/eros/app/khaos-oss/internal/logic/params/params.go:66
  5. 2). git.woa.com/khaos/eros/app/khaos-oss/internal/logic/params.(*sParams).GetParams
  6. /root/workspace/khaos/eros/app/khaos-oss/internal/logic/params/params.go:36
  7. 3). git.woa.com/khaos/eros/app/khaos-oss/internal/controller.(*cParams).GetOne