Browse code

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 <dug@us.ibm.com>

Doug Davis authored on 2015/12/16 11:24:10
Showing 2 changed files
... ...
@@ -161,15 +161,18 @@ func Load(configDir string) (*ConfigFile, error) {
161 161
 	if _, err := os.Stat(configFile.filename); err == nil {
162 162
 		file, err := os.Open(configFile.filename)
163 163
 		if err != nil {
164
-			return &configFile, err
164
+			return &configFile, fmt.Errorf("%s - %v", configFile.filename, err)
165 165
 		}
166 166
 		defer file.Close()
167 167
 		err = configFile.LoadFromReader(file)
168
+		if err != nil {
169
+			err = fmt.Errorf("%s - %v", configFile.filename, err)
170
+		}
168 171
 		return &configFile, err
169 172
 	} else if !os.IsNotExist(err) {
170 173
 		// if file is there but we can't stat it for any reason other
171 174
 		// than it doesn't exist then stop
172
-		return &configFile, err
175
+		return &configFile, fmt.Errorf("%s - %v", configFile.filename, err)
173 176
 	}
174 177
 
175 178
 	// Can't find latest config file so check for the old one
... ...
@@ -179,12 +182,12 @@ func Load(configDir string) (*ConfigFile, error) {
179 179
 	}
180 180
 	file, err := os.Open(confFile)
181 181
 	if err != nil {
182
-		return &configFile, err
182
+		return &configFile, fmt.Errorf("%s - %v", confFile, err)
183 183
 	}
184 184
 	defer file.Close()
185 185
 	err = configFile.LegacyLoadFromReader(file)
186 186
 	if err != nil {
187
-		return &configFile, err
187
+		return &configFile, fmt.Errorf("%s - %v", confFile, err)
188 188
 	}
189 189
 
190 190
 	if configFile.HTTPHeaders == nil {
... ...
@@ -138,8 +138,9 @@ email`: "Invalid Auth config file",
138 138
 		}
139 139
 
140 140
 		config, err := Load(tmpHome)
141
-		if err == nil || err.Error() != expectedError {
142
-			t.Fatalf("Should have failed, got: %q, %q", config, err)
141
+		// Use Contains instead of == since the file name will change each time
142
+		if err == nil || !strings.Contains(err.Error(), expectedError) {
143
+			t.Fatalf("Should have failed\nConfig: %v\nGot: %v\nExpected: %v", config, err, expectedError)
143 144
 		}
144 145
 
145 146
 	}
... ...
@@ -207,7 +208,8 @@ func TestOldJsonInvalid(t *testing.T) {
207 207
 	}
208 208
 
209 209
 	config, err := Load(tmpHome)
210
-	if err == nil || err.Error() != "Invalid auth configuration file" {
210
+	// Use Contains instead of == since the file name will change each time
211
+	if err == nil || !strings.Contains(err.Error(), "Invalid auth configuration file") {
211 212
 		t.Fatalf("Expected an error got : %v, %v", config, err)
212 213
 	}
213 214
 }