Skip to content

Commit d96f8b7

Browse files
committed
cli/config: improve error when failing to parse config file
The format had a stray colon and space included. While fixing that, also updating the error message to clarify the error happened while parsing the file (not so much "loading" it). Before: WARNING: Error loading config file: /root/.docker/config.json: : json: cannot unmarshal bool into Go struct field ConfigFile.features of type string After: WARNING: Error parsing config file (/root/.docker/config.json): json: cannot unmarshal bool into Go struct field ConfigFile.features of type string Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 062eecf commit d96f8b7

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

cli/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func load(configDir string) (*configfile.ConfigFile, error) {
143143
defer file.Close()
144144
err = configFile.LoadFromReader(file)
145145
if err != nil {
146-
err = errors.Wrapf(err, "loading config file: %s: ", filename)
146+
err = errors.Wrapf(err, "parsing config file (%s)", filename)
147147
}
148148
return configFile, err
149149
}

cli/config/config_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ func TestEmptyJSON(t *testing.T) {
118118
saveConfigAndValidateNewFormat(t, config, tmpHome)
119119
}
120120

121+
func TestMalformedJSON(t *testing.T) {
122+
tmpHome := t.TempDir()
123+
124+
fn := filepath.Join(tmpHome, ConfigFileName)
125+
err := os.WriteFile(fn, []byte("{"), 0o600)
126+
assert.NilError(t, err)
127+
128+
_, err = Load(tmpHome)
129+
assert.Check(t, is.ErrorContains(err, fmt.Sprintf(`parsing config file (%s):`, fn)))
130+
}
131+
121132
func TestNewJSON(t *testing.T) {
122133
tmpHome := t.TempDir()
123134

0 commit comments

Comments
 (0)