diff options
author | Jesse Hallam <jesse.hallam@gmail.com> | 2018-04-26 11:19:25 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-26 11:19:25 -0400 |
commit | 6d50d836f538253e2d13d5ddb90495820f9cb259 (patch) | |
tree | 2d61042647dec793be5f6e4e87f095dfef658f14 /utils/config.go | |
parent | d3f09b54e2be65981f0496938f9d5f97507874e6 (diff) | |
download | chat-6d50d836f538253e2d13d5ddb90495820f9cb259.tar.gz chat-6d50d836f538253e2d13d5ddb90495820f9cb259.tar.bz2 chat-6d50d836f538253e2d13d5ddb90495820f9cb259.zip |
MM-10232, MM-10259: Improve error handling from invalid json (#8668)
* MM-10232: improve error handling from malformed slash command responses
Switch to json.Unmarshal, which doesn't obscure JSON parse failures like
json.Decode. The latter is primarily designed for streams of JSON, not
necessarily unmarshalling just a single object.
* rework HumanizedJsonError to expose Line and Character discretely
* MM-10259: pinpoint line and character where json config error occurs
* tweak HumanizeJsonError to accept err first
Diffstat (limited to 'utils/config.go')
-rw-r--r-- | utils/config.go | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/utils/config.go b/utils/config.go index fa436f70d..51b7ea003 100644 --- a/utils/config.go +++ b/utils/config.go @@ -4,6 +4,7 @@ package utils import ( + "bytes" "encoding/json" "fmt" "io" @@ -23,6 +24,7 @@ import ( "github.com/mattermost/mattermost-server/einterfaces" "github.com/mattermost/mattermost-server/model" + "github.com/mattermost/mattermost-server/utils/jsonutils" ) const ( @@ -214,9 +216,19 @@ func (w *ConfigWatcher) Close() { // ReadConfig reads and parses the given configuration. func ReadConfig(r io.Reader, allowEnvironmentOverrides bool) (*model.Config, map[string]interface{}, error) { - v := newViper(allowEnvironmentOverrides) + // Pre-flight check the syntax of the configuration file to improve error messaging. + configData, err := ioutil.ReadAll(r) + if err != nil { + return nil, nil, err + } else { + var rawConfig interface{} + if err := json.Unmarshal(configData, &rawConfig); err != nil { + return nil, nil, jsonutils.HumanizeJsonError(err, configData) + } + } - if err := v.ReadConfig(r); err != nil { + v := newViper(allowEnvironmentOverrides) + if err := v.ReadConfig(bytes.NewReader(configData)); err != nil { return nil, nil, err } |