Skip to content

Commit

Permalink
Merge 026f538 into d739ca9
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jul 23, 2015
2 parents d739ca9 + 026f538 commit 2a8e068
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
sudo: false
language: node_js
node_js:
- 'iojs-2'
Expand Down
15 changes: 11 additions & 4 deletions lib/urllib.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,11 +668,18 @@ function parseJSON(data) {
};
try {
result.data = JSON.parse(data);
} catch (e) {
if (e.name === 'SyntaxError') {
e.name = 'JSONResponseFormatError';
} catch (err) {
if (err.name === 'SyntaxError') {
err.name = 'JSONResponseFormatError';
}
result.error = e;
if (data.length > 1024) {
// show 0~512 ... -512~end data
err.message += ' (data json format: ' +
JSON.stringify(data.slice(0, 512)) + ' ...skip... ' + JSON.stringify(data.slice(data.length - 512)) + ')';
} else {
err.message += ' (data json format: ' + JSON.stringify(data) + ')';
}
result.error = err;
}
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"request",
"https"
],
"author": "fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)",
"author": "fengmk2 <m@fengmk2.com> (http://fengmk2.com)",
"homepage": "http://github.com/node-modules/urllib",
"main": "lib/urllib.js",
"files": [
Expand Down
6 changes: 5 additions & 1 deletion test/fixtures/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ var server = http.createServer(function (req, res) {
return res.end(req.url);
} else if (req.url === '/wrongjson') {
res.writeHeader(200);
return res.end('{"foo":""');
return res.end(new Buffer('{"foo":""'));
} else if (req.url === '/wrongjson-gbk') {
res.setHeader('content-type', 'application/json; charset=gbk');
res.writeHeader(200);
return res.end(fs.readFileSync(__filename));
} else if (req.url === '/writestream') {
var s = fs.createReadStream(__filename);
return s.pipe(res);
Expand Down
15 changes: 14 additions & 1 deletion test/urllib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,13 +430,26 @@ describe('urllib.test.js', function () {
}, function (err, data, res) {
should.exist(err);
err.name.should.equal('JSONResponseFormatError');
err.message.should.containEql('Unexpected end of input, GET http://127.0.0.1:');
err.message.should.containEql('Unexpected end of input (data json format: "{\\"foo\\":\\"\\""), GET http://127.0.0.1:');
res.should.status(200);
data.toString().should.equal('{"foo":""');
done();
});
});

it('should handle GET /wrongjson-gbk with dataType=json and data size > 1024', function (done) {
urllib.request(host + '/wrongjson-gbk', {
dataType: 'json'
}, function (err, data, res) {
should.exist(err);
err.name.should.equal('JSONResponseFormatError');
err.message.should.containEql('Unexpected token / (data json format: "/**!\\n * urllib - test/fixtures/server.js\\n');
err.message.should.containEql('" ...skip... "');
res.should.status(200);
done();
});
});

it('should support options.dataType=text', function (done) {
urllib.request(host + '/wrongjson', {
dataType: 'text'
Expand Down

0 comments on commit 2a8e068

Please sign in to comment.