NewWithOption
Custom Error Creation
-
Description: Used for creating custom-configured error objects.
-
Format:
// 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 -
Example:
func ExampleNewWithOption() {
err := gerror.NewWithOption(gerror.Option{
Text: "this feature is disabled in this storage",
Code: gcode.CodeNotSupported,
})
}Where
Option
is defined as:// 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
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 Specifier | Output Content |
---|---|
%v , %s | Prints all hierarchical error information, returning a complete string, with multiple levels concatenated by : . |
%-v , %-s | Prints the current level of error information, returning a string. |
%+s | Prints a complete list of stack information. |
%+v | Prints all hierarchical error information strings, along with complete stack information, equivalent to %s\n%+s . |
Usage example:
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)
}
After execution, the output example:
%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
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:
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)
}
After execution, the output example:
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
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 Mode | Default | Description |
---|---|---|
brief | Yes | Brief Mode. When printing error stacks, framework-related stacks will not be printed. |
detail | Detailed 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.
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
While in brief mode (brief
), the stack information will not print the framework stack information. For example:
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