NewWithOption Custom Error Creation

  • Description: Used for creating custom-configured error objects.

  • Format:

    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
  • Example:

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

    Where Option is defined as:

    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 Formatting

As shown in previous examples, using the %+v print format can print out complete stack information. Of course, the gerror.Error object supports multiple fmt formats:

Format SpecifierOutput Content
%v, %sPrints all hierarchical error information, returning a complete string, with multiple levels concatenated by :.
%-v, %-sPrints the current level of error information, returning a string.
%+sPrints a complete list of stack information.
%+vPrints all hierarchical error information strings, along with complete stack information, equivalent to %s\n%+s.

Usage example:

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

After execution, the output example:

  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

Log Component Support

The glog log component naturally supports stack printing for gerror errors. This support is not strongly coupled but is supported through the fmt formatting print interface.

Usage example:

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

After execution, the output example:

  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

Stack Printing Mode

This feature is provided from version v2.6.0 of the framework.

When printing stack information, the error component supports users specifying stack printing information mode via an environment variable (GF_GERROR_STACK_MODE) or a command line argument (gf.gerror.stack.mode):

Stack ModeDefaultDescription
briefYesBrief Mode. When printing error stacks, framework-related stacks will not be printed.
detailDetailed Mode. When printing error stacks, the complete framework component code call chain will be printed.

In detailed mode (detail), the complete framework stack information will be printed in the error object. For instance, in the following stack error example, you can see that most of the framework stack information is not very meaningful for error localization, and as a developer, more concern is given to the stack information at their own code level.

  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

While in brief mode (brief), the stack information will not print the framework stack information. For example:

  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