Skip to content

Commit

Permalink
Merge pull request #773 from ParsePlatform/flovilmart.fixHttpRequest
Browse files Browse the repository at this point in the history
Graceful fails on httpRequest
  • Loading branch information
flovilmart committed Mar 2, 2016
2 parents 4b7447e + 8eff444 commit 1bfee45
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
15 changes: 15 additions & 0 deletions spec/HTTPRequest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,20 @@ describe("httpRequest", () => {
var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'X-Custom-Header': 'my-header'});
expect(result).toEqual({"foo": "bar", "bar": "baz"});
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();
}
});
})
});
8 changes: 7 additions & 1 deletion src/cloud-code/httpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down

0 comments on commit 1bfee45

Please sign in to comment.