| 2989 | }); |
| 2990 | } |
| 2991 | _processResponse(res, options) { |
| 2992 | return __awaiter(this, void 0, void 0, function* () { |
| 2993 | return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { |
| 2994 | const statusCode = res.message.statusCode || 0; |
| 2995 | const response = { |
| 2996 | statusCode, |
| 2997 | result: null, |
| 2998 | headers: {} |
| 2999 | }; |
| 3000 | // not found leads to null obj returned |
| 3001 | if (statusCode === HttpCodes.NotFound) { |
| 3002 | resolve(response); |
| 3003 | } |
| 3004 | // get the result from the body |
| 3005 | function dateTimeDeserializer(key, value) { |
| 3006 | if (typeof value === 'string') { |
| 3007 | const a = new Date(value); |
| 3008 | if (!isNaN(a.valueOf())) { |
| 3009 | return a; |
| 3010 | } |
| 3011 | } |
| 3012 | return value; |
| 3013 | } |
| 3014 | let obj; |
| 3015 | let contents; |
| 3016 | try { |
| 3017 | contents = yield res.readBody(); |
| 3018 | if (contents && contents.length > 0) { |
| 3019 | if (options && options.deserializeDates) { |
| 3020 | obj = JSON.parse(contents, dateTimeDeserializer); |
| 3021 | } |
| 3022 | else { |
| 3023 | obj = JSON.parse(contents); |
| 3024 | } |
| 3025 | response.result = obj; |
| 3026 | } |
| 3027 | response.headers = res.message.headers; |
| 3028 | } |
| 3029 | catch (err) { |
| 3030 | // Invalid resource (contents not json); leaving result obj null |
| 3031 | } |
| 3032 | // note that 3xx redirects are handled by the http layer. |
| 3033 | if (statusCode > 299) { |
| 3034 | let msg; |
| 3035 | // if exception/error in body, attempt to get better error |
| 3036 | if (obj && obj.message) { |
| 3037 | msg = obj.message; |
| 3038 | } |
| 3039 | else if (contents && contents.length > 0) { |
| 3040 | // it may be the case that the exception is in the body message as string |
| 3041 | msg = contents; |
| 3042 | } |
| 3043 | else { |
| 3044 | msg = `Failed request: (${statusCode})`; |
| 3045 | } |
| 3046 | const err = new HttpClientError(msg, statusCode); |
| 3047 | err.result = response.result; |
| 3048 | reject(err); |