|
18 | 18 | } |
19 | 19 |
|
20 | 20 | 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); |
22 | 28 |
|
23 | 29 | if (_.isString(responseBody)) { |
24 | 30 | try { |
|
27 | 33 | catch (err) {} |
28 | 34 | } |
29 | 35 |
|
30 | | - log('Handling response of ' + method + ' ' + object + ': ' + rawRequest.url.href + (rawRequest.url.href.indexOf(rawRequest.url.query) === -1 ? '?' + rawRequest.url.query : '')); |
31 | | - |
32 | 36 | 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; |
36 | 39 | } |
37 | | - else if (statusCodeSeries == '4' || statusCodeSeries == '5') { |
| 40 | + else if (statusCodeGroup === 4 || statusCodeGroup === 5) { |
38 | 41 | var errorObject = new Error(); |
39 | 42 |
|
40 | 43 | if (_.isObject(rawResponse._error)) { |
|
57 | 60 | } |
58 | 61 | } |
59 | 62 |
|
60 | | - if (_.isFunction(callback)) { |
61 | | - callback(errorObject, null, null, rawRequest, rawResponse); |
62 | | - } |
| 63 | + callback(errorObject, null, null, rawRequest, rawResponse); |
| 64 | + return; |
63 | 65 | } |
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; |
66 | 69 | } |
67 | 70 |
|
| 71 | + callback(null, responseBody.data || {}, responseBody.additional_data || {}, rawRequest, rawResponse); |
68 | 72 | }; |
69 | 73 |
|
70 | 74 | // GET /items |
|
0 commit comments