Cleanup config load error handling

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2018-03-06 13:44:06 -05:00
parent 7c8b5708eb
commit 789acb526c
2 changed files with 9 additions and 6 deletions

View File

@ -75,18 +75,18 @@ func Load(configDir string) (*configfile.ConfigFile, error) {
if _, err := os.Stat(filename); err == nil { if _, err := os.Stat(filename); err == nil {
file, err := os.Open(filename) file, err := os.Open(filename)
if err != nil { if err != nil {
return configFile, errors.Errorf("%s - %v", filename, err) return configFile, errors.Wrap(err, filename)
} }
defer file.Close() defer file.Close()
err = configFile.LoadFromReader(file) err = configFile.LoadFromReader(file)
if err != nil { if err != nil {
err = errors.Errorf("%s - %v", filename, err) err = errors.Wrap(err, filename)
} }
return configFile, err return configFile, err
} else if !os.IsNotExist(err) { } else if !os.IsNotExist(err) {
// if file is there but we can't stat it for any reason other // if file is there but we can't stat it for any reason other
// than it doesn't exist then stop // than it doesn't exist then stop
return configFile, errors.Errorf("%s - %v", filename, err) return configFile, errors.Wrap(err, filename)
} }
// Can't find latest config file so check for the old one // Can't find latest config file so check for the old one
@ -96,12 +96,12 @@ func Load(configDir string) (*configfile.ConfigFile, error) {
} }
file, err := os.Open(confFile) file, err := os.Open(confFile)
if err != nil { if err != nil {
return configFile, errors.Errorf("%s - %v", confFile, err) return configFile, errors.Wrap(err, confFile)
} }
defer file.Close() defer file.Close()
err = configFile.LegacyLoadFromReader(file) err = configFile.LegacyLoadFromReader(file)
if err != nil { if err != nil {
return configFile, errors.Errorf("%s - %v", confFile, err) return configFile, errors.Wrap(err, confFile)
} }
return configFile, nil return configFile, nil
} }

View File

@ -2,6 +2,7 @@ package config
import ( import (
"bytes" "bytes"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -13,6 +14,7 @@ import (
"github.com/docker/docker/pkg/homedir" "github.com/docker/docker/pkg/homedir"
"github.com/gotestyourself/gotestyourself/assert" "github.com/gotestyourself/gotestyourself/assert"
is "github.com/gotestyourself/gotestyourself/assert/cmp" is "github.com/gotestyourself/gotestyourself/assert/cmp"
"github.com/pkg/errors"
) )
func setupConfigDir(t *testing.T) (string, func()) { func setupConfigDir(t *testing.T) (string, func()) {
@ -77,7 +79,8 @@ func TestEmptyFile(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
_, err = Load(tmpHome) _, err = Load(tmpHome)
assert.ErrorContains(t, err, "EOF") assert.Equal(t, errors.Cause(err), io.EOF)
assert.ErrorContains(t, err, ConfigFileName)
} }
func TestEmptyJSON(t *testing.T) { func TestEmptyJSON(t *testing.T) {