summaryrefslogtreecommitdiffstats
path: root/model/command_response.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 /model/command_response.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 'model/command_response.go')
-rw-r--r--model/command_response.go24
1 files changed, 15 insertions, 9 deletions
diff --git a/model/command_response.go b/model/command_response.go
index cac7e8452..1ed5286de 100644
--- a/model/command_response.go
+++ b/model/command_response.go
@@ -8,6 +8,8 @@ import (
"io"
"io/ioutil"
"strings"
+
+ "github.com/mattermost/mattermost-server/utils/jsonutils"
)
const (
@@ -31,14 +33,14 @@ func (o *CommandResponse) ToJson() string {
return string(b)
}
-func CommandResponseFromHTTPBody(contentType string, body io.Reader) *CommandResponse {
+func CommandResponseFromHTTPBody(contentType string, body io.Reader) (*CommandResponse, error) {
if strings.TrimSpace(strings.Split(contentType, ";")[0]) == "application/json" {
return CommandResponseFromJson(body)
}
if b, err := ioutil.ReadAll(body); err == nil {
- return CommandResponseFromPlainText(string(b))
+ return CommandResponseFromPlainText(string(b)), nil
}
- return nil
+ return nil, nil
}
func CommandResponseFromPlainText(text string) *CommandResponse {
@@ -47,15 +49,19 @@ func CommandResponseFromPlainText(text string) *CommandResponse {
}
}
-func CommandResponseFromJson(data io.Reader) *CommandResponse {
- decoder := json.NewDecoder(data)
- var o CommandResponse
+func CommandResponseFromJson(data io.Reader) (*CommandResponse, error) {
+ b, err := ioutil.ReadAll(data)
+ if err != nil {
+ return nil, err
+ }
- if err := decoder.Decode(&o); err != nil {
- return nil
+ var o CommandResponse
+ err = json.Unmarshal(b, &o)
+ if err != nil {
+ return nil, jsonutils.HumanizeJsonError(err, b)
}
o.Attachments = StringifySlackFieldValue(o.Attachments)
- return &o
+ return &o, nil
}