From fe3aab6c5f9286adb9bc2a3ada5ff9a5b6fa56c1 Mon Sep 17 00:00:00 2001 From: leonid zharikov Date: Tue, 4 Apr 2023 18:17:00 +0300 Subject: [PATCH 1/2] feat: Add visibility and context to error handling - Make error more visible for easier identification - Add additional context to the error --- viper.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/viper.go b/viper.go index cdde717..459c58f 100644 --- a/viper.go +++ b/viper.go @@ -8,6 +8,7 @@ package viper import ( "context" "errors" + "fmt" "os" "strings" @@ -148,14 +149,20 @@ func (b *Bundle) provideViper(ctx context.Context, flagSet *pflag.FlagSet) (_ *v var configFile string if configFile, err = flagSet.GetString("config"); err != nil { - return nil, err + return nil, fmt.Errorf("unable to get config flag value: %w", err) } if len(configFile) > 0 { b.viper.SetConfigFile(configFile) } - return b.viper, b.viper.ReadInConfig() + err = b.viper.ReadInConfig() + if err != nil { + return nil, fmt.Errorf("unable to read config file: '%s' : %w", + configFile, err) + } + + return b.viper, nil } func (b *Bundle) provideFlagSet() (*pflag.FlagSet, error) { From b5eaad51949c859265064fd9be2a5e33972acbc8 Mon Sep 17 00:00:00 2001 From: Sergey Novichkov Date: Sun, 9 Apr 2023 20:33:40 +0300 Subject: [PATCH 2/2] Update viper.go --- viper.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/viper.go b/viper.go index 459c58f..7688737 100644 --- a/viper.go +++ b/viper.go @@ -149,7 +149,7 @@ func (b *Bundle) provideViper(ctx context.Context, flagSet *pflag.FlagSet) (_ *v var configFile string if configFile, err = flagSet.GetString("config"); err != nil { - return nil, fmt.Errorf("unable to get config flag value: %w", err) + return nil, fmt.Errorf("unable to get config flag value : %w", err) } if len(configFile) > 0 { @@ -158,7 +158,7 @@ func (b *Bundle) provideViper(ctx context.Context, flagSet *pflag.FlagSet) (_ *v err = b.viper.ReadInConfig() if err != nil { - return nil, fmt.Errorf("unable to read config file: '%s' : %w", + return nil, fmt.Errorf("unable to read config file : '%s' : %w", configFile, err) }