From 6d50d836f538253e2d13d5ddb90495820f9cb259 Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Thu, 26 Apr 2018 11:19:25 -0400 Subject: 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 --- utils/config_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'utils/config_test.go') diff --git a/utils/config_test.go b/utils/config_test.go index fbac577ee..11b110367 100644 --- a/utils/config_test.go +++ b/utils/config_test.go @@ -4,6 +4,7 @@ package utils import ( + "bytes" "io/ioutil" "os" "path/filepath" @@ -23,6 +24,19 @@ func TestConfig(t *testing.T) { InitTranslations(cfg.LocalizationSettings) } +func TestReadConfig(t *testing.T) { + TranslationsPreInit() + + _, _, err := ReadConfig(bytes.NewReader([]byte(``)), false) + require.EqualError(t, err, "parsing error at line 1, character 1: unexpected end of JSON input") + + _, _, err = ReadConfig(bytes.NewReader([]byte(` + { + malformed + `)), false) + require.EqualError(t, err, "parsing error at line 3, character 5: invalid character 'm' looking for beginning of object key string") +} + func TestTimezoneConfig(t *testing.T) { TranslationsPreInit() supportedTimezones := LoadTimezones("timezones.json") -- cgit v1.2.3-1-g7c22