Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Fixes #897: Adds error handling to the configuration file parsing pro…
Browse files Browse the repository at this point in the history
…cess by adding more information to the error messsages (if any) that are returned when the configuration file cannot be parsed correctly
  • Loading branch information
Tom McSweeney committed May 4, 2016
1 parent 170c8d3 commit e332eab
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 34 deletions.
10 changes: 5 additions & 5 deletions control/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,23 @@ func (c *Config) UnmarshalJSON(data []byte) error {
switch k {
case "max_running_plugins":
if err := json.Unmarshal(v, &(c.MaxRunningPlugins)); err != nil {
return err
return fmt.Errorf("%v (while parsing 'control::max_running_plugins')", err)
}
case "plugin_trust_level":
if err := json.Unmarshal(v, &(c.PluginTrust)); err != nil {
return err
return fmt.Errorf("%v (while parsing 'control::plugin_trust_level')", err)
}
case "auto_discover_path":
if err := json.Unmarshal(v, &(c.AutoDiscoverPath)); err != nil {
return err
return fmt.Errorf("%v (while parsing 'control::auto_discover_path')", err)
}
case "keyring_paths":
if err := json.Unmarshal(v, &(c.KeyringPaths)); err != nil {
return err
return fmt.Errorf("%v (while parsing 'control::keyring_paths')", err)
}
case "cache_expiration":
if err := json.Unmarshal(v, &(c.CacheExpiration)); err != nil {
return err
return fmt.Errorf("%v (while parsing 'control::cache_expiration')", err)
}
case "plugins":
if err := json.Unmarshal(v, c.Plugins); err != nil {
Expand Down
14 changes: 7 additions & 7 deletions mgmt/rest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,31 +200,31 @@ func (c *Config) UnmarshalJSON(data []byte) error {
switch k {
case "enable":
if err := json.Unmarshal(v, &(c.Enable)); err != nil {
return err
return fmt.Errorf("%v (while parsing 'restapi::enable')", err)
}
case "port":
if err := json.Unmarshal(v, &(c.Port)); err != nil {
return err
return fmt.Errorf("%v (while parsing 'restapi::port')", err)
}
case "https":
if err := json.Unmarshal(v, &(c.HTTPS)); err != nil {
return err
return fmt.Errorf("%v (while parsing 'restapi::https')", err)
}
case "rest_certificate":
if err := json.Unmarshal(v, &(c.RestCertificate)); err != nil {
return err
return fmt.Errorf("%v (while parsing 'restapi::rest_certificate')", err)
}
case "rest_key":
if err := json.Unmarshal(v, &(c.RestKey)); err != nil {
return err
return fmt.Errorf("%v (while parsing 'restapi::rest_key')", err)
}
case "rest_auth":
if err := json.Unmarshal(v, &(c.RestAuth)); err != nil {
return err
return fmt.Errorf("%v (while parsing 'restapi::rest_auth')", err)
}
case "rest_auth_password":
if err := json.Unmarshal(v, &(c.RestAuthPassword)); err != nil {
return err
return fmt.Errorf("%v (while parsing 'restapi::rest_auth_password')", err)
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions mgmt/tribe/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package tribe

import (
"encoding/json"
"fmt"
"net"
"os"
"time"
Expand Down Expand Up @@ -93,23 +94,23 @@ func (c *Config) UnmarshalJSON(data []byte) error {
switch k {
case "name":
if err := json.Unmarshal(v, &(c.Name)); err != nil {
return err
return fmt.Errorf("%v (while parsing 'tribe::name')", err)
}
case "enable":
if err := json.Unmarshal(v, &(c.Enable)); err != nil {
return err
return fmt.Errorf("%v (while parsing 'tribe::enable')", err)
}
case "bind_addr":
if err := json.Unmarshal(v, &(c.BindAddr)); err != nil {
return err
return fmt.Errorf("%v (while parsing 'tribe::bind_addr')", err)
}
case "bind_port":
if err := json.Unmarshal(v, &(c.BindPort)); err != nil {
return err
return fmt.Errorf("%v (while parsing 'tribe::bind_port')", err)
}
case "seed":
if err := json.Unmarshal(v, &(c.Seed)); err != nil {
return err
return fmt.Errorf("%v (while parsing 'tribe::seed')", err)
}
}
}
Expand Down
27 changes: 16 additions & 11 deletions pkg/cfgfile/cfgfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,33 @@ limitations under the License.
package cfgfile

import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"

"github.com/ghodss/yaml"
)

//
// Read an input configuration file, parsing it (as YAML or JSON)
// into the input 'interface{}'', v
func Read(path string, v interface{}) error {
// read bytes from file
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}

// Try to unmarshal the byte array, first in YAML, then JSON. If both
// fail to unmarshal the blob read above, then return an error.
if err := yaml.Unmarshal(b, v); err == nil {
return nil
}
if err := json.Unmarshal(b, v); err == nil {
return nil
// parse the byte-stream read (above); Note that we can parse both JSON
// and YAML using the same yaml.Unmarshal() method since a JSON string is
// a valid YAML string (but the converse is not true)
if parseErr := yaml.Unmarshal(b, v); parseErr != nil {
// remove any YAML-specific prefix that might have been added by then
// yaml.Unmarshal() method or JSON-specific prefix that might have been
// added if the resulting JSON string could not be marshalled into our
// input interface correctly (note, if there is no match to either of
// these prefixes then the error message will be passed through unchanged)
tmpErr := strings.TrimPrefix(parseErr.Error(), "error converting YAML to JSON: yaml: ")
errRet := strings.TrimPrefix(tmpErr, "error unmarshaling JSON: json: ")
return fmt.Errorf("Error while parsing configuration file: %v", errRet)
}
return fmt.Errorf("Unknown file type")
return nil
}
9 changes: 6 additions & 3 deletions scheduler/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ limitations under the License.

package scheduler

import "encoding/json"
import (
"encoding/json"
"fmt"
)

// default configuration values
const (
Expand Down Expand Up @@ -60,11 +63,11 @@ func (c *Config) UnmarshalJSON(data []byte) error {
switch k {
case "work_manager_queue_size":
if err := json.Unmarshal(v, &(c.WorkManagerQueueSize)); err != nil {
return err
return fmt.Errorf("%v (while parsing 'scheduler::work_manager_queue_size')", err)
}
case "work_manager_pool_size":
if err := json.Unmarshal(v, &(c.WorkManagerPoolSize)); err != nil {
return err
return fmt.Errorf("%v (while parsing 'scheduler::work_manager_pool_size')", err)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions snapd.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,15 +722,15 @@ func (c *Config) UnmarshalJSON(data []byte) error {
switch k {
case "log_level":
if err := json.Unmarshal(v, &(c.LogLevel)); err != nil {
return err
return fmt.Errorf("%v (while parsing 'log_level')", err)
}
case "gomaxprocs":
if err := json.Unmarshal(v, &(c.GoMaxProcs)); err != nil {
return err
return fmt.Errorf("%v (while parsing 'gomaxprocs')", err)
}
case "log_path":
if err := json.Unmarshal(v, &(c.LogPath)); err != nil {
return err
return fmt.Errorf("%v (while parsing 'log_path')", err)
}
case "control":
if err := json.Unmarshal(v, c.Control); err != nil {
Expand Down

0 comments on commit e332eab

Please sign in to comment.