Graceful fails on httpRequest

This commit is contained in:
Florent Vilmart
2016-03-02 16:43:44 -05:00
parent c7503fc654
commit 8eff44410f
2 changed files with 22 additions and 1 deletions

View File

@@ -36,6 +36,12 @@ module.exports = function(options) {
options.followRedirect = options.followRedirects == true;
request(options, (error, response, body) => {
if (error) {
if (callbacks.error) {
callbacks.error(error);
}
return promise.reject(error);
}
var httpResponse = {};
httpResponse.status = response.statusCode;
httpResponse.headers = response.headers;
@@ -46,7 +52,7 @@ module.exports = function(options) {
httpResponse.data = JSON.parse(response.body);
} catch (e) {}
// Consider <200 && >= 400 as errors
if (error || httpResponse.status <200 || httpResponse.status >=400) {
if (httpResponse.status < 200 || httpResponse.status >= 400) {
if (callbacks.error) {
callbacks.error(httpResponse);
}