Skip to content

Commit

Permalink
deal with http errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Mortimerp9 authored and rvagg committed Feb 23, 2017
1 parent 5aa95e9 commit 6d98f7b
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions jsonist.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ var url = require('url')
, xtend = require('xtend')


function HttpError (status, response) {
Error.call(this)
this.status = status
this.response = response
Error.captureStackTrace(this, arguments.callee)
}

HttpError.prototype = Object.create(Error.prototype)
HttpError.prototype.constructor = HttpError


function collector (uri, options, callback) {
var request = makeRequest(uri, options)
, redirect = null
Expand Down Expand Up @@ -39,10 +50,17 @@ function collector (uri, options, callback) {
try {
ret = JSON.parse(data.toString())
} catch (e) {
var err = new SyntaxError('JSON parse error: ' + e.message, e)
err.data = data
err.response = request.response
return callback(err)
if (request.response.statusCode >= 300) {
err = new HttpError(request.response.statusCode, request.response)
} else {
err = new SyntaxError('JSON parse error: ' + e.message, e)
err.statusCode = request.response.statusCode
err.data = data
err.response = request.response
return callback(err2)
}

return callback(err);
}

callback(null, ret, request.response)
Expand Down Expand Up @@ -109,3 +127,4 @@ 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

0 comments on commit 6d98f7b

Please sign in to comment.