From 5b0d6a0c8f231e218607d473b9a2169fadb82432 Mon Sep 17 00:00:00 2001 From: Avinash Sridharan Date: Thu, 30 Mar 2017 22:06:02 +0000 Subject: [PATCH] libcni: Improved error messages. Improved the error reporting to be more descriptive when configuration files are not filed while using the cni-tool. --- libcni/conf.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/libcni/conf.go b/libcni/conf.go index 4fa2bfed..c7738c66 100644 --- a/libcni/conf.go +++ b/libcni/conf.go @@ -16,7 +16,6 @@ package libcni import ( "encoding/json" - "errors" "fmt" "io/ioutil" "os" @@ -33,7 +32,13 @@ func (e NotFoundError) Error() string { return fmt.Sprintf(`no net configuration with name "%s" in %s`, e.Name, e.Dir) } -var NoConfigsFoundError = errors.New("no net configurations found") +type NoConfigsFoundError struct { + Dir string +} + +func (e NoConfigsFoundError) Error() string { + return fmt.Sprintf(`no net configurations found in %s`, e.Dir) +} func ConfFromBytes(bytes []byte) (*NetworkConfig, error) { conf := &NetworkConfig{Bytes: bytes} @@ -149,7 +154,7 @@ func LoadConf(dir, name string) (*NetworkConfig, error) { case err != nil: return nil, err case len(files) == 0: - return nil, NoConfigsFoundError + return nil, NoConfigsFoundError{Dir: dir} } sort.Strings(files) @@ -187,16 +192,12 @@ func LoadConfList(dir, name string) (*NetworkConfigList, error) { singleConf, err := LoadConf(dir, name) if err != nil { // A little extra logic so the error makes sense - switch { - // neither configlists nor config files found - case len(files) == 0 && err == NoConfigsFoundError: - return nil, err - // config lists found but no config files found - case len(files) != 0 && err == NoConfigsFoundError: + if _, ok := err.(NoConfigsFoundError); len(files) != 0 && ok { + // Config lists found but no config files found return nil, NotFoundError{dir, name} - default: // either not found or parse error - return nil, err } + + return nil, err } return ConfListFromConf(singleConf) }