Skip to content

Commit

Permalink
Merge pull request #1041 from kolodoz/master
Browse files Browse the repository at this point in the history
Set xhr.response instead of xhr.responseText to err.rawResponse
  • Loading branch information
kornelski authored Nov 13, 2016
2 parents e0af31c + 2943f35 commit 5e841d6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,16 @@ function Request(method, url) {
err.parse = true;
err.original = e;
// issue #675: return the raw response if the response parsing fails
err.rawResponse = self.xhr && self.xhr.responseText ? self.xhr.responseText : null;
// issue #876: return the http status code if the response parsing fails
err.statusCode = self.xhr && self.xhr.status ? self.xhr.status : null;
if (self.xhr) {
// ie9 doesn't have 'response' property
err.rawResponse = typeof self.xhr.responseType == 'undefined' ? self.xhr.responseText : self.xhr.response;
// issue #876: return the http status code if the response parsing fails
err.statusCode = self.xhr.status ? self.xhr.status : null;
} else {
err.rawResponse = null;
err.statusCode = null;
}

return self.callback(err);
}

Expand Down
12 changes: 12 additions & 0 deletions test/client/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,18 @@ if ('FormData' in window) {
})
.end();
});
it('get error status code and rawResponse on file download', function(next) {
request
.get('/arraybuffer-unauthorized')
.responseType('arraybuffer')
.end(function(err, res) {
assert(err.statusCode, 401);
assert(err.rawResponse instanceof ArrayBuffer);
var decodedString = String.fromCharCode.apply(null, new Uint8Array(err.rawResponse));
assert(decodedString, '{"message":"Authorization has been denied for this request."}');
next();
});
});
}

});
5 changes: 5 additions & 0 deletions test/support/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ app.get('/arraybuffer', function(req, res) {
res.send(content);
});

app.get('/arraybuffer-unauthorized', function(req, res) {
res.set('Content-Type', 'application/json');
res.status(401).send('{"message":"Authorization has been denied for this request."}');
});

app.post('/empty-body', bodyParser.text(), function(req, res) {
if (typeof req.body === 'object' && Object.keys(req.body).length === 0) {
res.sendStatus(204);
Expand Down

0 comments on commit 5e841d6

Please sign in to comment.