Skip to content

Commit 428d76a

Browse files
committed
Graceful fails on httpRequest
1 parent c7503fc commit 428d76a

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

spec/HTTPRequest.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -177,5 +177,20 @@ describe("httpRequest", () => {
177177
var result = httpRequest.encodeBody({"foo": "bar", "bar": "baz"}, {'X-Custom-Header': 'my-header'});
178178
expect(result).toEqual({"foo": "bar", "bar": "baz"});
179179
done();
180+
});
181+
182+
it("should fail gracefully", (done) => {
183+
httpRequest({
184+
url: "http://not a good url",
185+
success: function() {
186+
fail("should not succeed");
187+
done();
188+
},
189+
error: function(error) {
190+
expect(error).not.toBeUndefined();
191+
expect(error).not.toBeNull();
192+
done();
193+
}
194+
});
180195
})
181196
});

src/cloud-code/httpRequest.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ module.exports = function(options) {
3636
options.followRedirect = options.followRedirects == true;
3737

3838
request(options, (error, response, body) => {
39+
if (error) {
40+
if (callbacks.error) {
41+
callbacks.error(error);
42+
}
43+
return promise.reject(error);
44+
}
3945
var httpResponse = {};
4046
httpResponse.status = response.statusCode;
4147
httpResponse.headers = response.headers;
@@ -46,7 +52,7 @@ module.exports = function(options) {
4652
httpResponse.data = JSON.parse(response.body);
4753
} catch (e) {}
4854
// Consider <200 && >= 400 as errors
49-
if (error || httpResponse.status <200 || httpResponse.status >=400) {
55+
if (httpResponse.status <200 || httpResponse.status >=400) {
5056
if (callbacks.error) {
5157
callbacks.error(httpResponse);
5258
}

0 commit comments

Comments
 (0)