diff --git a/.travis.yml b/.travis.yml index 3681ff17..a7990451 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,4 @@ +sudo: false language: node_js node_js: - 'iojs-2' diff --git a/lib/urllib.js b/lib/urllib.js index d2141e38..89d71274 100644 --- a/lib/urllib.js +++ b/lib/urllib.js @@ -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; } diff --git a/package.json b/package.json index db92471e..5cf7c6ab 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "request", "https" ], - "author": "fengmk2 (http://fengmk2.github.com)", + "author": "fengmk2 (http://fengmk2.com)", "homepage": "http://github.com/node-modules/urllib", "main": "lib/urllib.js", "files": [ diff --git a/test/fixtures/server.js b/test/fixtures/server.js index e5b3bbc6..904e6e29 100644 --- a/test/fixtures/server.js +++ b/test/fixtures/server.js @@ -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); diff --git a/test/urllib.test.js b/test/urllib.test.js index c9135afe..e736694c 100644 --- a/test/urllib.test.js +++ b/test/urllib.test.js @@ -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'