Merge pull request #773 from ParsePlatform/flovilmart.fixHttpRequest
Graceful fails on httpRequest
This commit is contained in:
@@ -177,5 +177,20 @@ describe("httpRequest", () => {
|
|||||||
var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'X-Custom-Header': 'my-header'});
|
var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'X-Custom-Header': 'my-header'});
|
||||||
expect(result).toEqual({"foo": "bar", "bar": "baz"});
|
expect(result).toEqual({"foo": "bar", "bar": "baz"});
|
||||||
done();
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should fail gracefully", (done) => {
|
||||||
|
httpRequest({
|
||||||
|
url: "http://not a good url",
|
||||||
|
success: function() {
|
||||||
|
fail("should not succeed");
|
||||||
|
done();
|
||||||
|
},
|
||||||
|
error: function(error) {
|
||||||
|
expect(error).not.toBeUndefined();
|
||||||
|
expect(error).not.toBeNull();
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
});
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -36,6 +36,12 @@ module.exports = function(options) {
|
|||||||
options.followRedirect = options.followRedirects == true;
|
options.followRedirect = options.followRedirects == true;
|
||||||
|
|
||||||
request(options, (error, response, body) => {
|
request(options, (error, response, body) => {
|
||||||
|
if (error) {
|
||||||
|
if (callbacks.error) {
|
||||||
|
callbacks.error(error);
|
||||||
|
}
|
||||||
|
return promise.reject(error);
|
||||||
|
}
|
||||||
var httpResponse = {};
|
var httpResponse = {};
|
||||||
httpResponse.status = response.statusCode;
|
httpResponse.status = response.statusCode;
|
||||||
httpResponse.headers = response.headers;
|
httpResponse.headers = response.headers;
|
||||||
@@ -46,7 +52,7 @@ module.exports = function(options) {
|
|||||||
httpResponse.data = JSON.parse(response.body);
|
httpResponse.data = JSON.parse(response.body);
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
// Consider <200 && >= 400 as errors
|
// Consider <200 && >= 400 as errors
|
||||||
if (error || httpResponse.status <200 || httpResponse.status >=400) {
|
if (httpResponse.status < 200 || httpResponse.status >= 400) {
|
||||||
if (callbacks.error) {
|
if (callbacks.error) {
|
||||||
callbacks.error(httpResponse);
|
callbacks.error(httpResponse);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user