Skip to content

Commit

Permalink
error.rawResponse: use responseText for old browsers and response for…
Browse files Browse the repository at this point in the history
… new when responseType is not 'text'
  • Loading branch information
Pavel Ayusheev authored and Pavel Ayusheev committed Aug 12, 2016
1 parent 4e18cc0 commit 2943f35
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.response ? self.xhr.response : 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 @@ -229,6 +229,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 2943f35

Please sign in to comment.