diff --git a/errors/gerror/gerror.go b/errors/gerror/gerror.go index ac11e323d..282dcb40a 100644 --- a/errors/gerror/gerror.go +++ b/errors/gerror/gerror.go @@ -241,22 +241,30 @@ func WrapCodeSkipf(code gcode.Code, skip int, err error, format string, args ... } // Code returns the error code of current error. -// It returns CodeNil if it has no error code or it does not implements interface Code. +// It returns CodeNil if it has no error code neither it does not implement interface Code. func Code(err error) gcode.Code { - if err != nil { - if e, ok := err.(iCode); ok { - return e.Code() - } + if err == nil { + return gcode.CodeNil + } + if e, ok := err.(iCode); ok { + return e.Code() + } + if e, ok := err.(iNext); ok { + return Code(e.Next()) } return gcode.CodeNil } // Cause returns the root cause error of `err`. func Cause(err error) error { - if err != nil { - if e, ok := err.(iCause); ok { - return e.Cause() - } + if err == nil { + return nil + } + if e, ok := err.(iCause); ok { + return e.Cause() + } + if e, ok := err.(iNext); ok { + return Cause(e.Next()) } return err } diff --git a/errors/gerror/gerror_error.go b/errors/gerror/gerror_error.go index c69f01836..e3491b7bf 100644 --- a/errors/gerror/gerror_error.go +++ b/errors/gerror/gerror_error.go @@ -65,6 +65,9 @@ func (err *Error) Code() gcode.Code { if err == nil { return gcode.CodeNil } + if err.code == gcode.CodeNil { + return Code(err.Next()) + } return err.code } diff --git a/os/gcfg/gcfg.go b/os/gcfg/gcfg.go index 0e080afab..5342f56e3 100644 --- a/os/gcfg/gcfg.go +++ b/os/gcfg/gcfg.go @@ -13,6 +13,8 @@ import ( "github.com/gogf/gf/v2/container/gmap" "github.com/gogf/gf/v2/container/gvar" + "github.com/gogf/gf/v2/errors/gcode" + "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/internal/command" "github.com/gogf/gf/v2/internal/intlog" "github.com/gogf/gf/v2/internal/utils" @@ -140,7 +142,7 @@ func (c *Config) Get(ctx context.Context, pattern string, def ...interface{}) (* // Fetching Rules: Environment arguments are in uppercase format, eg: GF_PACKAGE_VARIABLE. func (c *Config) GetWithEnv(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error) { value, err := c.Get(ctx, pattern) - if err != nil { + if err != nil && gerror.Code(err) != gcode.CodeNotFound { return nil, err } if value == nil { @@ -162,7 +164,7 @@ func (c *Config) GetWithEnv(ctx context.Context, pattern string, def ...interfac // Fetching Rules: Command line arguments are in lowercase format, eg: gf.package.variable. func (c *Config) GetWithCmd(ctx context.Context, pattern string, def ...interface{}) (*gvar.Var, error) { value, err := c.Get(ctx, pattern) - if err != nil { + if err != nil && gerror.Code(err) != gcode.CodeNotFound { return nil, err } if value == nil { diff --git a/os/gcfg/gcfg_adapter_file_path.go b/os/gcfg/gcfg_adapter_file_path.go index fa65ecca5..b23c682fc 100644 --- a/os/gcfg/gcfg_adapter_file_path.go +++ b/os/gcfg/gcfg_adapter_file_path.go @@ -219,7 +219,7 @@ func (c *AdapterFile) GetFilePath(fileName ...string) (path string, err error) { } else { buffer.WriteString(fmt.Sprintf(`cannot find config file "%s" with no path configured`, usedFileName)) } - err = gerror.New(buffer.String()) + err = gerror.NewCode(gcode.CodeNotFound, buffer.String()) } return } diff --git a/os/gcfg/gcfg_z_example_test.go b/os/gcfg/gcfg_z_example_test.go new file mode 100644 index 000000000..a64a2e4ca --- /dev/null +++ b/os/gcfg/gcfg_z_example_test.go @@ -0,0 +1,66 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package gcfg_test + +import ( + "fmt" + "os" + + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/os/gcmd" + "github.com/gogf/gf/v2/os/gctx" + "github.com/gogf/gf/v2/os/genv" +) + +func ExampleConfig_GetWithEnv() { + var ( + key = `ENV_TEST` + ctx = gctx.New() + ) + v, err := g.Cfg().GetWithEnv(ctx, key) + if err != nil { + panic(err) + } + fmt.Printf("env:%s\n", v) + if err = genv.Set(key, "gf"); err != nil { + panic(err) + } + v, err = g.Cfg().GetWithEnv(ctx, key) + if err != nil { + panic(err) + } + fmt.Printf("env:%s", v) + + // Output: + // env: + // env:gf +} + +func ExampleConfig_GetWithCmd() { + var ( + key = `cmd.test` + ctx = gctx.New() + ) + v, err := g.Cfg().GetWithCmd(ctx, key) + if err != nil { + panic(err) + } + fmt.Printf("cmd:%s\n", v) + // Re-Initialize custom command arguments. + os.Args = append(os.Args, fmt.Sprintf(`--%s=yes`, key)) + gcmd.Init(os.Args...) + // Retrieve the configuration and command option again. + v, err = g.Cfg().GetWithCmd(ctx, key) + if err != nil { + panic(err) + } + fmt.Printf("cmd:%s", v) + + // Output: + // cmd: + // cmd:yes +}