summaryrefslogtreecommitdiffstats
path: root/utils/jsonutils/json.go
diff options
context:
space:
mode:
authorJesse Hallam <jesse.hallam@gmail.com>2018-04-26 11:19:25 -0400
committerGitHub <noreply@github.com>2018-04-26 11:19:25 -0400
commit6d50d836f538253e2d13d5ddb90495820f9cb259 (patch)
tree2d61042647dec793be5f6e4e87f095dfef658f14 /utils/jsonutils/json.go
parentd3f09b54e2be65981f0496938f9d5f97507874e6 (diff)
downloadchat-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/jsonutils/json.go')
-rw-r--r--utils/jsonutils/json.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/utils/jsonutils/json.go b/utils/jsonutils/json.go
new file mode 100644
index 000000000..da77a2b6b
--- /dev/null
+++ b/utils/jsonutils/json.go
@@ -0,0 +1,56 @@
+// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package jsonutils
+
+import (
+ "bytes"
+ "encoding/json"
+
+ "github.com/pkg/errors"
+)
+
+type HumanizedJsonError struct {
+ Err error
+ Line int
+ Character int
+}
+
+func (e *HumanizedJsonError) Error() string {
+ return e.Err.Error()
+}
+
+// HumanizeJsonError extracts error offsets and annotates the error with useful context
+func HumanizeJsonError(err error, data []byte) error {
+ if syntaxError, ok := err.(*json.SyntaxError); ok {
+ return NewHumanizedJsonError(syntaxError, data, syntaxError.Offset)
+ } else if unmarshalError, ok := err.(*json.UnmarshalTypeError); ok {
+ return NewHumanizedJsonError(unmarshalError, data, unmarshalError.Offset)
+ } else {
+ return err
+ }
+}
+
+func NewHumanizedJsonError(err error, data []byte, offset int64) *HumanizedJsonError {
+ if err == nil {
+ return nil
+ }
+
+ if offset < 0 || offset > int64(len(data)) {
+ return &HumanizedJsonError{
+ Err: errors.Wrapf(err, "invalid offset %d", offset),
+ }
+ }
+
+ lineSep := []byte{'\n'}
+
+ line := bytes.Count(data[:offset], lineSep) + 1
+ lastLineOffset := bytes.LastIndex(data[:offset], lineSep)
+ character := int(offset) - (lastLineOffset + 1) + 1
+
+ return &HumanizedJsonError{
+ Line: line,
+ Character: character,
+ Err: errors.Wrapf(err, "parsing error at line %d, character %d", line, character),
+ }
+}