NewOption自定义的错误创建

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

    1. NewOption(option Option) error
  • 示例:

    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 格式化

通过以上示例我们可以看到,通过%+v的打印格式可以打印出完整的堆栈信息,当然gerror.Error对象支持多种fmt格式:

格式符输出内容
%v, %s打印所有的层级错误信息,构成完成的字符串返回,多个层级使用:拼接。
%-v, %-s打印当前层级的错误信息,返回字符串。
%+s打印完整的堆栈信息列表。
%+v打印所有的层级错误信息字符串,以及完整的堆栈信息,等同于%s\n%+s

使用示例:

  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/gogf/gf/v2/errors/gerror"
  6. )
  7. func main() {
  8. var err error
  9. err = errors.New("sql error")
  10. err = gerror.Wrap(err, "adding failed")
  11. err = gerror.Wrap(err, "api calling failed")
  12. fmt.Printf(" %%s: %s\n", err)
  13. fmt.Printf("%%-s: %-s\n", err)
  14. fmt.Println("%+s: ")
  15. fmt.Printf("%+s\n", err)
  16. }
  17. // Output:
  18. // %s: api calling failed: adding failed: sql error
  19. // %-s: api calling failed
  20. // %+s:
  21. // 1. api calling failed
  22. // 1). main.main
  23. // /Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/other/test.go:14
  24. // 2. adding failed
  25. // 1). main.main
  26. // /Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/other/test.go:13
  27. // 3. sql error

日志输出支持

glog日志管理模块天然支持对gerror错误堆栈打印支持,这种支持不是强耦合性的,而是通过fmt格式化打印接口支持的。

使用示例:

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

堆栈打印模式

该特性从框架v2.6.0版本开始提供。

错误组件在打印堆栈信息时支持通过环境变量(GF_GERROR_STACK_MODE)或者命令行启动参数(gf.gerror.stack.mode)指定堆栈打印信息模式:

堆栈模式是否默认说明
brief简略模式。错误堆栈打印时,不会打印框架相关的堆栈。
detail
详情模式。错误堆栈打印时,会打印完整的框架组件代码调用链路。

在详情模式(detail)下,将会打印错误对象中完整的框架堆栈信息。可以看到,大部分框架堆栈信息其实是没有太大意义的,作为开发者,更多关心的自身代码层面的堆栈信息。

  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
  8. /root/workspace/khaos/eros/app/khaos-oss/internal/controller/params.go:21
  9. 4). github.com/gogf/gf/v2/net/ghttp.(*middleware).callHandlerFunc.func1
  10. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:152
  11. 5). github.com/gogf/gf/v2/net/ghttp.niceCallFunc
  12. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_func.go:55
  13. 6). github.com/gogf/gf/v2/net/ghttp.(*middleware).callHandlerFunc
  14. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:129
  15. 7). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1
  16. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:75
  17. 8). github.com/gogf/gf/v2/util/gutil.TryCatch
  18. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/util/gutil/gutil.go:56
  19. 9). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next
  20. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:49
  21. 10). git.woa.com/khaos/eros/app/khaos-oss/internal/logic/middleware.(*sMiddleware).CheckLimit
  22. /root/workspace/khaos/eros/app/khaos-oss/internal/logic/middleware/middleware.go:27
  23. 11). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1.5
  24. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:96
  25. 12). github.com/gogf/gf/v2/net/ghttp.niceCallFunc
  26. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_func.go:55
  27. 13). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1
  28. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:95
  29. 14). github.com/gogf/gf/v2/util/gutil.TryCatch
  30. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/util/gutil/gutil.go:56
  31. 15). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next
  32. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:49
  33. 16). git.woa.com/khaos/eros/utility/server.MiddlewareCommonResponse
  34. /root/workspace/khaos/eros/utility/server/server_common.go:14
  35. 17). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1.5
  36. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:96
  37. 18). github.com/gogf/gf/v2/net/ghttp.niceCallFunc
  38. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_func.go:55
  39. 19). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1
  40. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:95
  41. 20). github.com/gogf/gf/v2/util/gutil.TryCatch
  42. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/util/gutil/gutil.go:56
  43. 21). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next
  44. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:49
  45. 22). git.woa.com/khaos/eros/utility/server.MiddlewareLogging
  46. /root/workspace/khaos/eros/utility/server/server.go:46
  47. 23). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1.5
  48. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:96
  49. 24). github.com/gogf/gf/v2/net/ghttp.niceCallFunc
  50. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_func.go:55
  51. 25). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1
  52. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:95
  53. 26). github.com/gogf/gf/v2/util/gutil.TryCatch
  54. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/util/gutil/gutil.go:56
  55. 27). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next
  56. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:49
  57. 28). github.com/gogf/gf/v2/net/ghttp.MiddlewareCORS
  58. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_middleware_cors.go:12
  59. 29). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1.5
  60. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:96
  61. 30). github.com/gogf/gf/v2/net/ghttp.niceCallFunc
  62. /root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_func.go:55
  63. 2. rpc error: code = NotFound desc = cluster.khaos.tencent.com "tdxxxx-a2c378bd" not found

在简略模式(brief)下,堆栈信息中将不会打印框架的堆栈信息。例如:

  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
  8. /root/workspace/khaos/eros/app/khaos-oss/internal/controller/params.go:21
  9. 2. rpc error: code = NotFound desc = cluster.khaos.tencent.com "tdxxxx-a2c378bd" not found