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
Этот коммит содержится в:
Jesse Hallam
2018-04-26 11:19:25 -04:00
коммит произвёл GitHub
родитель d3f09b54e2
Коммит 6d50d836f5
12 изменённых файлов: 487 добавлений и 109 удалений

Просмотреть файл

@@ -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
}