From 8f765c90fc1161b2cad2fa7d04c035d869e7d128 Mon Sep 17 00:00:00 2001 From: narayana shanubhogh Date: Sun, 25 Apr 2021 20:41:13 +0530 Subject: [PATCH] returning object --- lib/rest/axios.js | 13 +++++++++++++ lib/rest/request.js | 2 ++ 2 files changed, 15 insertions(+) diff --git a/lib/rest/axios.js b/lib/rest/axios.js index d323f07..78348d5 100644 --- a/lib/rest/axios.js +++ b/lib/rest/axios.js @@ -207,6 +207,19 @@ export function Axios(config) { } }) .catch(function (error) { + const exceptionClass = { + 400: Exceptions.InvalidRequestError, + 401: Exceptions.AuthenticationError, + 404: Exceptions.ResourceNotFoundError, + 405: Exceptions.InvalidRequestError, + 500: Exceptions.ServerError, + } [error.response.status] || Error; + if (!_.inRange(error.response.status, 200, 300)) { + let body = error.response.data; + if (typeof body === 'object') { + reject(new exceptionClass(body)); + } + } reject(error.stack + '\n' + JSON.stringify(error.response.data)); }) } diff --git a/lib/rest/request.js b/lib/rest/request.js index 336d18f..94f1c72 100644 --- a/lib/rest/request.js +++ b/lib/rest/request.js @@ -199,6 +199,8 @@ export function Request(config) { if (!_.inRange(response.statusCode, 200, 300)) { body = body || response.body; if (typeof body === 'object') { + reject(new exceptionClass(JSON.stringify(body))); + } else { reject(new exceptionClass(body)); } }