From 5aac0047092ee0868aecbe8ea835dd4a41024611 Mon Sep 17 00:00:00 2001 From: Pat Cavit Date: Tue, 18 Jul 2017 22:53:17 -0700 Subject: [PATCH] refactor: XHR errors have response/code fields So that there's no chance of data loss and it's trivial to get the response code (because it's hella useful) Fixes #1866 Fixes #1876 --- request/request.js | 3 ++- request/tests/test-request.js | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/request/request.js b/request/request.js index 76e218c9..ac5f3391 100644 --- a/request/request.js +++ b/request/request.js @@ -93,7 +93,8 @@ module.exports = function($window, Promise) { } else { var error = new Error(xhr.responseText) - for (var key in response) error[key] = response[key] + error.code = xhr.status + error.response = response reject(error) } } diff --git a/request/tests/test-request.js b/request/tests/test-request.js index 7f965498..94e7e172 100644 --- a/request/tests/test-request.js +++ b/request/tests/test-request.js @@ -458,9 +458,10 @@ o.spec("xhr", function() { xhr({method: "GET", url: "/item"}).catch(function(e) { o(e instanceof Error).equals(true) o(e.message).equals(JSON.stringify({error: "error"})) + o(e.code).equals(500) }).then(done) }) - o("extends Error with JSON response", function(done) { + o("adds response to Error", function(done) { mock.$defineRoutes({ "GET /item": function() { return {status: 500, responseText: JSON.stringify({message: "error", stack: "error on line 1"})} @@ -468,8 +469,8 @@ o.spec("xhr", function() { }) xhr({method: "GET", url: "/item"}).catch(function(e) { o(e instanceof Error).equals(true) - o(e.message).equals("error") - o(e.stack).equals("error on line 1") + o(e.response.message).equals("error") + o(e.response.stack).equals("error on line 1") }).then(done) }) o("rejects on non-JSON server error", function(done) {