Comparing version 0.1.2 to 0.2.0
39
index.js
@@ -5,2 +5,4 @@ if (!global.Promise) { | ||
var util = require('util'); | ||
if (!Array.prototype.last) { | ||
@@ -39,5 +41,18 @@ Array.prototype.last = function() { | ||
/* | ||
* Error instance wrapping HTTP error responses | ||
* | ||
* Has the same properties as the original response. | ||
*/ | ||
function HTTPErrorResponse(response) { | ||
for (var key in response) { | ||
this[key] = response[key]; | ||
} | ||
this.stack = this.toString(); | ||
} | ||
util.inherits(HTTPErrorResponse, Error); | ||
function wrap(method) { | ||
return function (url, options) { | ||
var options = getOptions(url, options); | ||
options = getOptions(url, options); | ||
if (method) { | ||
@@ -50,4 +65,3 @@ options.method = method; | ||
if (!err) { | ||
err = new Error('Response error'); | ||
err.response = { | ||
err = new HTTPErrorResponse({ | ||
status: 500, | ||
@@ -57,8 +71,16 @@ body: { | ||
} | ||
}; | ||
}); | ||
} else { | ||
err = new HTTPErrorResponse({ | ||
status: 500, | ||
body: { | ||
type: 'internal_error' | ||
}, | ||
stack: err.stack | ||
}); | ||
} | ||
return reject(err); | ||
} | ||
if (res.body && res.headers | ||
&& /^application\/json/.test(res.headers['content-type'])) { | ||
if (res.body && res.headers && | ||
/^application\/json/.test(res.headers['content-type'])) { | ||
res.body = JSON.parse(res.body); | ||
@@ -74,6 +96,3 @@ } | ||
if (ourRes.status >= 400) { | ||
var e = new Error('Response error:\n' | ||
+ JSON.stringify(ourRes, null, 2)); | ||
e.response = ourRes; | ||
reject(e); | ||
reject(new HTTPErrorResponse(ourRes)); | ||
} else { | ||
@@ -80,0 +99,0 @@ resolve(ourRes); |
{ | ||
"name": "preq", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "Yet another promising request wrapper", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
3467
98