Skip to content

Wrapped parse call into try/catch block #875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,18 @@ Response.prototype.setHeaderProperties = function(header){

Response.prototype.parseBody = function(str){
var parse = request.parse[this.type];
return parse && str && (str.length || str instanceof Object)
? parse(str)
: null;
var result;

if (parse && str && (str.length || str instanceof Object)) {
try{
result = parse(str);
} catch(e) {
result = str;
}
} else {
result = null;
}
return result;
};

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1008,11 +1008,11 @@ Request.prototype.end = function(fn){
if (parse) {
try {
parse(res, function(err, obj){
if (err && !self._aborted) self.callback(err);
res.body = obj;
if (err && !self._aborted) self.callback(err, res);
});
} catch (err) {
self.callback(err);
self.callback(err, res);
return;
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/node/parsers/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ module.exports = function parseJSON(res, fn){
res.setEncoding('utf8');
res.on('data', function(chunk){ res.text += chunk;});
res.on('end', function(){
var body;
try {
var body = res.text && JSON.parse(res.text);
body = res.text && JSON.parse(res.text);
} catch (e) {
var err = e;
// issue #675: return the raw response if the response parsing fails
err.rawResponse = res.text || null;
body = res.text;
} finally {
fn(err, body);
}
Expand Down
6 changes: 3 additions & 3 deletions test/client/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var assert = require('assert');
var request = require('../../');

describe('request', function() {
this.timeout(10000);
this.timeout(15000);

it('request() error object', function(next) {
request('GET', '/error').end(function(err, res) {
Expand Down Expand Up @@ -73,8 +73,8 @@ it('GET invalid json', function(next) {
request
.get('/invalid-json')
.end(function(err, res) {
assert(err.parse);
assert.deepEqual(err.rawResponse, ")]}', {'header':{'code':200,'text':'OK','version':'1.0'},'data':'some data'}");
assert(res);
assert.deepEqual(res.body, ")]}', {'header':{'code':200,'text':'OK','version':'1.0'},'data':'some data'}");
next();
});
});
Expand Down
5 changes: 3 additions & 2 deletions test/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,12 @@ describe('res.body', function(){
});

describe('Invalid JSON response', function(){
it('should return the raw response', function(done){
it('should return the raw response in the request body', function(done){
request
.get(uri + '/invalid-json')
.end(function(err, res){
assert.deepEqual(err.rawResponse, ")]}', {'header':{'code':200,'text':'OK','version':'1.0'},'data':'some data'}");
assert(res);
assert.deepEqual(res.body, ")]}', {'header':{'code':200,'text':'OK','version':'1.0'},'data':'some data'}");
done();
});
});
Expand Down