NewWithOption
自定义的错误创建
-
说明:用于自定义配置的错误对象创建。
-
格式:
// NewWithOption creates and returns a custom error with Option.
// It is the senior usage for creating error, which is often used internally in framework.
NewWithOption(option Option) error -
示例:
func ExampleNewWithOption() {
err := gerror.NewWithOption(gerror.Option{
Text: "this feature is disabled in this storage",
Code: gcode.CodeNotSupported,
})
}其中,
Option
的定义如下:// Option is option for creating error.
type Option struct {
Error error // Wrapped error if any.
Stack bool // Whether recording stack information into error.
Text string // Error text, which is created by New* functions.
Code gcode.Code // Error code if necessary.
}
fmt
格式化
通过前面示例我们可以看到,通过 %+v
的打印格式可以打印出完整的堆栈信息,当然 gerror.Error
对象支持多种fmt
格式:
格式符 | 输出内容 |
---|---|
%v , %s | 打印所有的层级错误信息,构成完成的字符串返回,多个层级使用 : 拼接。 |
%-v , %-s | 打印当前层级的错误信息,返回字符串。 |
%+s | 打印完整的堆栈信息列表。 |
%+v | 打印所有的层级错误信息字符串,以及完整的堆栈信息,等同于 %s\n%+s 。 |
使用示例:
package main
import (
"errors"
"fmt"
"github.com/gogf/gf/v2/errors/gerror"
)
func main() {
var err error
err = errors.New("sql error")
err = gerror.Wrap(err, "adding failed")
err = gerror.Wrap(err, "api calling failed")
fmt.Printf(" %%s: %s\n", err)
fmt.Printf("%%-s: %-s\n", err)
fmt.Println("%+s: ")
fmt.Printf("%+s\n", err)
}
执行后,输出示例:
%s: api calling failed: adding failed: sql error
%-s: api calling failed
%+s:
1. api calling failed
1). main.main
/Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/other/test.go:14
2. adding failed
1). main.main
/Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/other/test.go:13
3. sql error
日志组件支持
glog
日志组件天然支持对 gerror
错误堆栈打印支持,这种支持不是强耦合性的,而是通过 fmt
格式化打印接口支持的。
使用示例:
package main
import (
"errors"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/errors/gerror"
)
func main() {
var err error
err = errors.New("sql error")
err = gerror.Wrap(err, "adding failed")
err = gerror.Wrap(err, "api calling failed")
g.Log().Printf("%+v", err)
}
执行后,输出示例:
2020-10-17 15:22:26.793 api calling failed: adding failed: sql error
1. api calling failed
1). main.main
/Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/other/test.go:14
2. adding failed
1). main.main
/Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/.example/other/test.go:13
3. sql error
堆栈打印模式
该特性从框架 v2.6.0
版本开始提供。
错误组件在打印堆栈信息时,支持使用者通过环境变量( GF_GERROR_STACK_MODE
)或者命令行启动参数( gf.gerror.stack.mode
)指定堆栈打印信息模式:
堆栈模式 | 是否默认 | 说明 |
---|---|---|
brief | 是 | 简略模式。错误堆栈打印时,不会打印框架相关的堆栈。 |
detail | 详情模式。错误堆栈打印时,会打印完整的框架组件代码调用链路。 |
在详情模式( detail
)下,将会打印错误对象中完整的框架堆栈信息。 比如,在以下错误堆栈示例中,可以看到,大部分框架堆栈信息对于错误定位来说,其实是没有太大意义的,作为开发者,更多关心的自身代码层面的堆栈信息。
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
1. GetParams failed: {ResourceId:tdxxxx-a2c378bd Component: Version:0}
1). github.com/khaos/eros/app/khaos-oss/internal/logic/params.(*sParams).doGetParamsJson
/root/workspace/khaos/eros/app/khaos-oss/internal/logic/params/params.go:66
2). github.com/khaos/eros/app/khaos-oss/internal/logic/params.(*sParams).GetParams
/root/workspace/khaos/eros/app/khaos-oss/internal/logic/params/params.go:36
3). github.com/khaos/eros/app/khaos-oss/internal/controller.(*cParams).GetOne
/root/workspace/khaos/eros/app/khaos-oss/internal/controller/params.go:21
4). github.com/gogf/gf/v2/net/ghttp.(*middleware).callHandlerFunc.func1
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:152
5). github.com/gogf/gf/v2/net/ghttp.niceCallFunc
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_func.go:55
6). github.com/gogf/gf/v2/net/ghttp.(*middleware).callHandlerFunc
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:129
7). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:75
8). github.com/gogf/gf/v2/util/gutil.TryCatch
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/util/gutil/gutil.go:56
9). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:49
10). github.com/khaos/eros/app/khaos-oss/internal/logic/middleware.(*sMiddleware).CheckLimit
/root/workspace/khaos/eros/app/khaos-oss/internal/logic/middleware/middleware.go:27
11). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1.5
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:96
12). github.com/gogf/gf/v2/net/ghttp.niceCallFunc
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_func.go:55
13). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:95
14). github.com/gogf/gf/v2/util/gutil.TryCatch
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/util/gutil/gutil.go:56
15). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:49
16). github.com/khaos/eros/utility/server.MiddlewareCommonResponse
/root/workspace/khaos/eros/utility/server/server_common.go:14
17). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1.5
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:96
18). github.com/gogf/gf/v2/net/ghttp.niceCallFunc
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_func.go:55
19). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:95
20). github.com/gogf/gf/v2/util/gutil.TryCatch
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/util/gutil/gutil.go:56
21). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:49
22). github.com/khaos/eros/utility/server.MiddlewareLogging
/root/workspace/khaos/eros/utility/server/server.go:46
23). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1.5
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:96
24). github.com/gogf/gf/v2/net/ghttp.niceCallFunc
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_func.go:55
25). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:95
26). github.com/gogf/gf/v2/util/gutil.TryCatch
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/util/gutil/gutil.go:56
27). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:49
28). github.com/gogf/gf/v2/net/ghttp.MiddlewareCORS
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_middleware_cors.go:12
29). github.com/gogf/gf/v2/net/ghttp.(*middleware).Next.func1.5
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_request_middleware.go:96
30). github.com/gogf/gf/v2/net/ghttp.niceCallFunc
/root/go/pkg/mod/github.com/gogf/gf/v2@v2.1.4/net/ghttp/ghttp_func.go:55
2. rpc error: code = NotFound desc = cluster.khaos.tencent.com "tdxxxx-a2c378bd" not found
而在简略模式( brief
)下,堆栈信息中将不会打印框架的堆栈信息。例如:
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
1. GetParams failed: {ResourceId:tdxxxx-a2c378bd Component: Version:0}
1). github.com/khaos/eros/app/khaos-oss/internal/logic/params.(*sParams).doGetParamsJson
/root/workspace/khaos/eros/app/khaos-oss/internal/logic/params/params.go:66
2). github.com/khaos/eros/app/khaos-oss/internal/logic/params.(*sParams).GetParams
/root/workspace/khaos/eros/app/khaos-oss/internal/logic/params/params.go:36
3). github.com/khaos/eros/app/khaos-oss/internal/controller.(*cParams).GetOne
/root/workspace/khaos/eros/app/khaos-oss/internal/controller/params.go:21
2. rpc error: code = NotFound desc = cluster.khaos.tencent.com "tdxxxx-a2c378bd" not found