summaryrefslogtreecommitdiffstats
path: root/api4
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 /api4
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 'api4')
-rw-r--r--api4/webhook.go12
-rw-r--r--api4/webhook_test.go10
2 files changed, 15 insertions, 7 deletions
diff --git a/api4/webhook.go b/api4/webhook.go
index 52c4ea331..a0e7b5785 100644
--- a/api4/webhook.go
+++ b/api4/webhook.go
@@ -509,11 +509,15 @@ func commandWebhook(c *Context, w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
id := params["id"]
- response := model.CommandResponseFromHTTPBody(r.Header.Get("Content-Type"), r.Body)
-
- err := c.App.HandleCommandWebhook(id, response)
+ response, err := model.CommandResponseFromHTTPBody(r.Header.Get("Content-Type"), r.Body)
if err != nil {
- c.Err = err
+ c.Err = model.NewAppError("commandWebhook", "web.command_webhook.parse.app_error", nil, err.Error(), http.StatusBadRequest)
+ return
+ }
+
+ appErr := c.App.HandleCommandWebhook(id, response)
+ if appErr != nil {
+ c.Err = appErr
return
}
diff --git a/api4/webhook_test.go b/api4/webhook_test.go
index 0a295b4b2..e983b6461 100644
--- a/api4/webhook_test.go
+++ b/api4/webhook_test.go
@@ -917,17 +917,21 @@ func TestCommandWebhooks(t *testing.T) {
t.Fatal(err)
}
- if resp, _ := http.Post(Client.Url+"/hooks/commands/123123123123", "application/json", bytes.NewBufferString("{\"text\":\"this is a test\"}")); resp.StatusCode != http.StatusNotFound {
+ if resp, _ := http.Post(Client.Url+"/hooks/commands/123123123123", "application/json", bytes.NewBufferString(`{"text":"this is a test"}`)); resp.StatusCode != http.StatusNotFound {
t.Fatal("expected not-found for non-existent hook")
}
+ if resp, err := http.Post(Client.Url+"/hooks/commands/"+hook.Id, "application/json", bytes.NewBufferString(`{"text":"invalid`)); err != nil || resp.StatusCode != http.StatusBadRequest {
+ t.Fatal(err)
+ }
+
for i := 0; i < 5; i++ {
- if resp, err := http.Post(Client.Url+"/hooks/commands/"+hook.Id, "application/json", bytes.NewBufferString("{\"text\":\"this is a test\"}")); err != nil || resp.StatusCode != http.StatusOK {
+ if resp, err := http.Post(Client.Url+"/hooks/commands/"+hook.Id, "application/json", bytes.NewBufferString(`{"text":"this is a test"}`)); err != nil || resp.StatusCode != http.StatusOK {
t.Fatal(err)
}
}
- if resp, _ := http.Post(Client.Url+"/hooks/commands/"+hook.Id, "application/json", bytes.NewBufferString("{\"text\":\"this is a test\"}")); resp.StatusCode != http.StatusBadRequest {
+ if resp, _ := http.Post(Client.Url+"/hooks/commands/"+hook.Id, "application/json", bytes.NewBufferString(`{"text":"this is a test"}`)); resp.StatusCode != http.StatusBadRequest {
t.Fatal("expected error for sixth usage")
}
}