From b0a18d57a73bf6b6bd2b7ea71de6065a0053e048 Mon Sep 17 00:00:00 2001 From: Doug Davis Date: Tue, 15 Dec 2015 18:24:10 -0800 Subject: [PATCH] Add the filename to the error message while trying to parse the config file Notice this while looking at #18634. W/o this extra text we're not sure if its complaining about the old or new config file. Signed-off-by: Doug Davis --- cliconfig/config.go | 11 +++++++---- cliconfig/config_test.go | 8 +++++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/cliconfig/config.go b/cliconfig/config.go index d3d109d122..e4ea948768 100644 --- a/cliconfig/config.go +++ b/cliconfig/config.go @@ -161,15 +161,18 @@ func Load(configDir string) (*ConfigFile, error) { if _, err := os.Stat(configFile.filename); err == nil { file, err := os.Open(configFile.filename) if err != nil { - return &configFile, err + return &configFile, fmt.Errorf("%s - %v", configFile.filename, err) } defer file.Close() err = configFile.LoadFromReader(file) + if err != nil { + err = fmt.Errorf("%s - %v", configFile.filename, err) + } return &configFile, err } else if !os.IsNotExist(err) { // if file is there but we can't stat it for any reason other // than it doesn't exist then stop - return &configFile, err + return &configFile, fmt.Errorf("%s - %v", configFile.filename, err) } // Can't find latest config file so check for the old one @@ -179,12 +182,12 @@ func Load(configDir string) (*ConfigFile, error) { } file, err := os.Open(confFile) if err != nil { - return &configFile, err + return &configFile, fmt.Errorf("%s - %v", confFile, err) } defer file.Close() err = configFile.LegacyLoadFromReader(file) if err != nil { - return &configFile, err + return &configFile, fmt.Errorf("%s - %v", confFile, err) } if configFile.HTTPHeaders == nil { diff --git a/cliconfig/config_test.go b/cliconfig/config_test.go index b6c8160001..147f5adbd6 100644 --- a/cliconfig/config_test.go +++ b/cliconfig/config_test.go @@ -138,8 +138,9 @@ email`: "Invalid Auth config file", } config, err := Load(tmpHome) - if err == nil || err.Error() != expectedError { - t.Fatalf("Should have failed, got: %q, %q", config, err) + // Use Contains instead of == since the file name will change each time + if err == nil || !strings.Contains(err.Error(), expectedError) { + t.Fatalf("Should have failed\nConfig: %v\nGot: %v\nExpected: %v", config, err, expectedError) } } @@ -207,7 +208,8 @@ func TestOldJsonInvalid(t *testing.T) { } config, err := Load(tmpHome) - if err == nil || err.Error() != "Invalid auth configuration file" { + // Use Contains instead of == since the file name will change each time + if err == nil || !strings.Contains(err.Error(), "Invalid auth configuration file") { t.Fatalf("Expected an error got : %v, %v", config, err) } }