From fd7cf8041e19b7cb7771ef6045a9bde9df6c09d4 Mon Sep 17 00:00:00 2001 From: Martti Laine Date: Mon, 6 Aug 2018 13:35:50 +0200 Subject: [PATCH] [request] Clearer error message for JSON deserialization failure (#2195) --- docs/change-log.md | 1 + request/request.js | 2 +- request/tests/test-request.js | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/change-log.md b/docs/change-log.md index e77780f8..8734fc55 100644 --- a/docs/change-log.md +++ b/docs/change-log.md @@ -32,6 +32,7 @@ - API: Introduction of `m.redraw.sync()` ([#1592](https://github.com/MithrilJS/mithril.js/pull/1592)) - API: Event handlers may also be objects with `handleEvent` methods ([#1939](https://github.com/MithrilJS/mithril.js/issues/1939)). - API: `m.route.link` accepts an optional `options` object ([#1930](https://github.com/MithrilJS/mithril.js/pull/1930)) +- API: `m.request` better error message on JSON parse error - ([#2195](https://github.com/MithrilJS/mithril.js/pull/2195), [@codeclown](https://github.com/codeclown)) - API: `m.request` supports `timeout` as attr - ([#1966](https://github.com/MithrilJS/mithril.js/pull/1966)) - Mocks: add limited support for the DOMParser API ([#2097](https://github.com/MithrilJS/mithril.js/pull/2097)) - API: add support for raw SVG in `m.trust()` string ([#2097](https://github.com/MithrilJS/mithril.js/pull/2097)) diff --git a/request/request.js b/request/request.js index 8424f889..37d92b31 100644 --- a/request/request.js +++ b/request/request.js @@ -162,7 +162,7 @@ module.exports = function($window, Promise) { function deserialize(data) { try {return data !== "" ? JSON.parse(data) : null} - catch (e) {throw new Error(data)} + catch (e) {throw new Error("Invalid JSON: " + data)} } function extract(xhr) {return xhr.responseText} diff --git a/request/tests/test-request.js b/request/tests/test-request.js index 52f840ef..6efa5ef2 100644 --- a/request/tests/test-request.js +++ b/request/tests/test-request.js @@ -494,7 +494,7 @@ o.spec("xhr", function() { } }) xhr({method: "GET", url: "/item"}).catch(function(e) { - o(e.message).equals("error") + o(e.message).equals("Invalid JSON: error") }).then(done) }) o("triggers all branched catches upon rejection", function(done) {