Comparing version 1.3.0 to 2.0.0
@@ -8,2 +8,11 @@ var url = require('url') | ||
function HttpError (message) { | ||
SyntaxError.call(this, message) | ||
Error.captureStackTrace(this, arguments.callee) | ||
} | ||
HttpError.prototype = Object.create(SyntaxError.prototype) | ||
HttpError.prototype.constructor = HttpError | ||
function collector (uri, options, callback) { | ||
@@ -38,3 +47,3 @@ var request = makeRequest(uri, options) | ||
var ret | ||
var ret, msg | ||
@@ -44,6 +53,10 @@ try { | ||
} catch (e) { | ||
var err = new SyntaxError('JSON parse error: ' + e.message, e) | ||
msg = 'JSON parse error: ' + e.message | ||
err = request.response.statusCode >= 300 ? new HttpError(msg) : new SyntaxError(msg) | ||
err.statusCode = request.response.statusCode | ||
err.data = data | ||
err.response = request.response | ||
return callback(err) | ||
return callback(err); | ||
} | ||
@@ -67,12 +80,12 @@ | ||
if (!options.method) | ||
if (typeof options.method != 'string') | ||
options.method = method | ||
if (!options.headers) | ||
if (typeof options.headers != 'object') | ||
options.headers = {} | ||
if (data && !options.headers['content-type']) | ||
if (data && typeof options.headers['content-type'] != 'string') | ||
options.headers['content-type'] = 'application/json' | ||
if (!options.headers['accept']) | ||
if (typeof options.headers['accept'] != 'string') | ||
options.headers['accept'] = 'application/json' | ||
@@ -112,5 +125,6 @@ | ||
module.exports.get = makeMethod('GET' , false) | ||
module.exports.post = makeMethod('POST' , true) | ||
module.exports.put = makeMethod('PUT' , true) | ||
module.exports.delete = makeMethod('DELETE' , false) | ||
module.exports.get = makeMethod('GET' , false) | ||
module.exports.post = makeMethod('POST' , true) | ||
module.exports.put = makeMethod('PUT' , true) | ||
module.exports.delete = makeMethod('DELETE' , false) | ||
module.exports.HttpError = HttpError |
{ | ||
"name": "jsonist", | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"description": "A simple wrapper around for dealing with JSON web APIs", | ||
@@ -5,0 +5,0 @@ "main": "jsonist.js", |
@@ -86,4 +86,16 @@ # jsonist | ||
## Error handling and bad JSON responses | ||
Server errors (i.e. response codes >= 300) are handled as standard responses. You can get the status code from the response object which is the third argument to the standard callback if you need to handle error responses in a different way. | ||
However, if any type of response returns data that is not JSON format, an error will be generated and passed as the first argument on the callback, with the following customisations: | ||
* If the status code from the server is >= 300, you will receive an error of type `jsonist.HttpError`, otherwise it will be of type `SyntaxError` indicating a bad JSON parse on a normal response. | ||
* The error will come with the following additional properties attached: | ||
- `data`: a `Buffer` containing the full response from the server | ||
- `response`: the full HTTP response object | ||
- `statusCode`: the status code received from the server (a short-cut to `response.statusCode`) | ||
## License | ||
**jsonist** is Copyright (c) 2014 Rod Vagg [@rvagg](https://github.com/rvagg) and licensed under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details. |
55
test.js
@@ -12,3 +12,3 @@ const test = require('tape') | ||
function testServer (serverResponse) { | ||
function testServer (serverResponse, statusCode) { | ||
var ee = new EE() | ||
@@ -27,3 +27,6 @@ , server = http.createServer(handler) | ||
res.writeHead(200, { 'content-type': 'application/json' }) | ||
res.writeHead( | ||
typeof statusCode == 'number' ? statusCode : 200 | ||
, { 'content-type': 'application/json' } | ||
) | ||
res.end(serverResponse || '') | ||
@@ -317,1 +320,49 @@ } | ||
}) | ||
test('server error, non-JSON', function (t) { | ||
t.plan(7) | ||
var responseText = 'there was an error' | ||
testServer(responseText, 501) | ||
.on('ready', function (url) { | ||
jsonist.get(url, function (err, data, response) { | ||
t.ok(err) | ||
t.ok(err instanceof jsonist.HttpError) | ||
t.equal(err.data.toString(), responseText, 'got correct response') | ||
t.equal(err.statusCode, 501, 'got correct statusCode') | ||
}) | ||
}) | ||
.on('request', function (req, data) { | ||
t.equal(req.method, 'GET', 'got GET request') | ||
t.equal(req.headers['accept'], 'application/json', 'got correct accept header') | ||
}) | ||
.on('close', t.ok.bind(t, true, 'ended')) | ||
}) | ||
test('server error, with-JSON', function (t) { | ||
t.plan(8) | ||
var responseDoc = 'there was an error' | ||
testServer(stringify(responseDoc), 501) | ||
.on('ready', function (url) { | ||
jsonist.get(url, function (err, data, response) { | ||
t.notOk(err, 'no error') | ||
t.deepEqual(data, responseDoc, 'got correct doc') | ||
t.ok(response, 'got response object') | ||
t.equal(response.statusCode, 501, 'got correct status code') | ||
t.equal( | ||
response && response.headers && response.headers['content-type'] | ||
, 'application/json', 'verified response object by content-type header' | ||
) | ||
}) | ||
}) | ||
.on('request', function (req, data) { | ||
t.equal(req.method, 'GET', 'got GET request') | ||
t.equal(req.headers['accept'], 'application/json', 'got correct accept header') | ||
}) | ||
.on('close', t.ok.bind(t, true, 'ended')) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24108
408
101