NewOption
自定义的错误创建
- 说明:用于自定义配置的错误对象创建。
示例:
func ExampleNewOption() {
err := gerror.NewOption(gerror.Option{
Text: "this feature is disabled in this storage",
Code: gcode.CodeNotSupported,
})
}
fmt
格式化
使用示例:
日志输出支持
使用示例:
package main
"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)
}
// Output:
// 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.2.0
版本开始,我们可以通过环境变量配置GF_GERROR_BRIEF=true
或者命令行启动参数--gf.gerror.brief=true
来启用错误堆栈的简略模式,在简略模式下,堆栈信息中将不会打印框架的堆栈信息。以上堆栈信息将会变为:
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). git.woa.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). git.woa.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). git.woa.com/khaos/eros/app/khaos-oss/internal/controller.(*cParams).GetOne