From 36e68948593404eaa27a26092391d048498ca435 Mon Sep 17 00:00:00 2001 From: Leo Horie Date: Fri, 24 Jun 2016 21:40:11 -0400 Subject: [PATCH] extend rejection reason w/ json error on request error --- request/request.js | 6 +++++- request/tests/test-xhr.js | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/request/request.js b/request/request.js index 3feedf15..488166c9 100644 --- a/request/request.js +++ b/request/request.js @@ -51,7 +51,11 @@ module.exports = function($window) { stream(response) } - else stream.error(new Error(xhr.responseText)) + else { + var error = new Error(xhr.responseText) + for (var key in response) error[key] = response[key] + stream.error(error) + } } catch (e) { stream.error(e) diff --git a/request/tests/test-xhr.js b/request/tests/test-xhr.js index 830a5625..bfbd1dca 100644 --- a/request/tests/test-xhr.js +++ b/request/tests/test-xhr.js @@ -260,9 +260,22 @@ 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"})) }).map(done) }) + o("extends Error with JSON response", function(done) { + mock.$defineRoutes({ + "GET /item": function(request) { + return {status: 500, responseText: JSON.stringify({message: "error", stack: "error on line 1"})} + } + }) + 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") + }).map(done) + }) o("rejects on non-JSON server error", function(done) { mock.$defineRoutes({ "GET /item": function(request) {