NewWithOption 自定义的错误创建

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

  • 格式:

    1. // NewWithOption creates and returns a custom error with Option.
    2. // It is the senior usage for creating error, which is often used internally in framework.
    3. NewWithOption(option Option) error
  • 示例:

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

    其中,Option的定义如下:

    1. // Option is option for creating error.
    2. type Option struct {
    3. Error error // Wrapped error if any.
    4. Stack bool // Whether recording stack information into error.
    5. Text string // Error text, which is created by New* functions.
    6. Code gcode.Code // Error code if necessary.
    7. }

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. }

执行后,输出示例:

  1. %s: api calling failed: adding failed: sql error
  2. %-s: api calling failed
  3. %+s:
  4. 1. api calling failed
  5. 1). main.main
  6. /Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/other/test.go:14
  7. 2. adding failed
  8. 1). main.main
  9. /Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/other/test.go:13
  10. 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. }

执行后,输出示例:

  1. 2020-10-17 15:22:26.793 api calling failed: adding failed: sql error
  2. 1. api calling failed
  3. 1). main.main
  4. /Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/other/test.go:14
  5. 2. adding failed
  6. 1). main.main
  7. /Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/other/test.go:13
  8. 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). github.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). github.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). github.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). github.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). github.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). github.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). github.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). github.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). github.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