Skip to content

Commit

Permalink
Adding statusCode to error response when JSON response is malformed
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdell committed Jan 29, 2016
1 parent 24eb1ac commit 449a852
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/client.js
Expand Up @@ -506,6 +506,8 @@ function Request(method, url) {
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;
return self.callback(err);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/node/parsers/json.js
Expand Up @@ -10,6 +10,8 @@ module.exports = function parseJSON(res, fn){
var err = e;
// issue #675: return the raw response if the response parsing fails
err.rawResponse = res.text || null;
// issue #876: return the http status code if the response parsing fails
err.statusCode = res.statusCode;
} finally {
fn(err, body);
}
Expand Down
1 change: 0 additions & 1 deletion test/client/request.js
Expand Up @@ -90,7 +90,6 @@ it('GET querystring empty objects', function(next){
});
});


it('GET querystring object .get(uri, obj)', function(next){
request
.get('/querystring', { search: 'Manny' })
Expand Down
9 changes: 9 additions & 0 deletions test/json.js
Expand Up @@ -148,6 +148,15 @@ describe('res.body', function(){
done();
});
});

it('should return the http status code', function(done){
request
.get(uri + '/invalid-json-forbidden')
.end(function(err, res){
assert.equal(err.statusCode, 403);
done();
});
});
});

if (doesntWorkInBrowserYet) describe('No content', function(){
Expand Down
5 changes: 5 additions & 0 deletions test/support/server.js
Expand Up @@ -151,6 +151,11 @@ app.get('/invalid-json', function(req, res) {
res.send(")]}', {'header':{'code':200,'text':'OK','version':'1.0'},'data':'some data'}");
});

app.get('/invalid-json-forbidden', function(req, res) {
res.set('content-type', 'application/json');
res.status(403).send("Forbidden");
});

app.get('/text', function(req, res){
res.send("just some text");
});
Expand Down

0 comments on commit 449a852

Please sign in to comment.