extend rejection reason w/ json error on request error

This commit is contained in:
Leo Horie 2016-06-24 21:40:11 -04:00
parent 6e3ce0aeb5
commit 36e6894859
2 changed files with 18 additions and 1 deletions

View file

@ -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)

View file

@ -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) {