diff --git a/spec/HTTPRequest.spec.js b/spec/HTTPRequest.spec.js index e599dd5d89..ad4e289ff9 100644 --- a/spec/HTTPRequest.spec.js +++ b/spec/HTTPRequest.spec.js @@ -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(); + } + }); }) }); diff --git a/src/cloud-code/httpRequest.js b/src/cloud-code/httpRequest.js index 2b5f9bff90..4e8ff65454 100644 --- a/src/cloud-code/httpRequest.js +++ b/src/cloud-code/httpRequest.js @@ -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); }