Skip to content

Commit b8e4a1b

Browse files
committed
fix restHandlers.genericResponse error handling
1 parent 7c9582f commit b8e4a1b

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

lib/restHandlers.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@
1818
}
1919

2020
RestHandlers.prototype.genericResponse = function(method, object, responseBody, callback, rawRequest, rawResponse) {
21-
var statusCodeSeries = (rawResponse.statusCode || 0).toString().substr(0, 1);
21+
log('Handling response of ' + method + ' ' + object + ': ' + rawRequest.url.href + (rawRequest.url.href.indexOf(rawRequest.url.query) === -1 ? '?' + rawRequest.url.query : ''));
22+
23+
if (!_.isFunction(callback)) {
24+
return;
25+
}
26+
27+
var statusCodeGroup = parseInt((rawResponse.statusCode || 0).toString().substr(0, 1), 10);
2228

2329
if (_.isString(responseBody)) {
2430
try {
@@ -27,14 +33,11 @@
2733
catch (err) {}
2834
}
2935

30-
log('Handling response of ' + method + ' ' + object + ': ' + rawRequest.url.href + (rawRequest.url.href.indexOf(rawRequest.url.query) === -1 ? '?' + rawRequest.url.query : ''));
31-
3236
if (responseBody.success === true) {
33-
if (_.isFunction(callback)) {
34-
callback(null, responseBody.data, responseBody.additional_data, rawRequest, rawResponse);
35-
}
37+
callback(null, responseBody.data, responseBody.additional_data, rawRequest, rawResponse);
38+
return;
3639
}
37-
else if (statusCodeSeries == '4' || statusCodeSeries == '5') {
40+
else if (statusCodeGroup === 4 || statusCodeGroup === 5) {
3841
var errorObject = new Error();
3942

4043
if (_.isObject(rawResponse._error)) {
@@ -57,14 +60,15 @@
5760
}
5861
}
5962

60-
if (_.isFunction(callback)) {
61-
callback(errorObject, null, null, rawRequest, rawResponse);
62-
}
63+
callback(errorObject, null, null, rawRequest, rawResponse);
64+
return;
6365
}
64-
else if (_.isFunction(callback)) {
65-
callback(null, responseBody.data || {}, responseBody.additional_data || {}, rawRequest, rawResponse);
66+
else if (responseBody.error) {
67+
callback(responseBody.error, responseBody.data || {}, responseBody.additional_data || {}, rawRequest, rawResponse);
68+
return;
6669
}
6770

71+
callback(null, responseBody.data || {}, responseBody.additional_data || {}, rawRequest, rawResponse);
6872
};
6973

7074
// GET /items

0 commit comments

Comments
 (0)