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)); } }