From dda00548e8e9f8b35316eb5f541fc0260bc89692 Mon Sep 17 00:00:00 2001 From: narayana shanubhogh Date: Tue, 27 Apr 2021 13:27:22 +0530 Subject: [PATCH] exception handle --- lib/rest/axios.js | 8 +++++++- lib/utils/restException.js | 7 ++++++- package.json | 1 + 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/rest/axios.js b/lib/rest/axios.js index a2173d1..86505dd 100644 --- a/lib/rest/axios.js +++ b/lib/rest/axios.js @@ -2,6 +2,7 @@ import axios from 'axios'; import queryString from 'querystring'; import * as Exceptions from '../utils/exceptions'; import * as _ from "lodash"; +var HttpsProxyAgent = require('https-proxy-agent'); export function Axios(config) { let auth = 'Basic ' + new Buffer(config.authId + ':' + config.authToken) @@ -137,7 +138,7 @@ export function Axios(config) { } if (typeof config.proxy !== 'undefined') { - options.proxy = config.proxy; + options.httpsAgent = new HttpsProxyAgent(config.proxy); } if (typeof config.timeout !== 'undefined') { @@ -218,6 +219,11 @@ export function Axios(config) { let body = error.response.data; if (typeof body === 'object') { reject(new exceptionClass(error)); + } else { + if (error.response.status >= 500) { + reject(new Exceptions.ServerError(error)); + } + reject(new exceptionClass(error)); } } reject(error.stack + '\n' + JSON.stringify(error.response.data)); diff --git a/lib/utils/restException.js b/lib/utils/restException.js index e0c7f28..d41e200 100644 --- a/lib/utils/restException.js +++ b/lib/utils/restException.js @@ -2,8 +2,13 @@ class PlivoRestError extends Error { constructor(response) { response = response.response + var body; super('[HTTP ' + response.status + '] Failed to execute request'); - const body = typeof response.data === 'string' ? JSON.parse(response.data) : response.data; + try { + body = typeof response.data === 'string' ? JSON.parse(response.data) : response.data; + } catch (err) { + body = {"error": response.data, "api_id": ''} + } this.status = response.status; this.statusText = response.statusText this.message = body.error; diff --git a/package.json b/package.json index c7458e5..dbf571e 100644 --- a/package.json +++ b/package.json @@ -61,6 +61,7 @@ "@types/node": "^14.14.14", "axios": "^0.21.1", "base-64": "^0.1.0", + "https-proxy-agent": "^5.0.0", "build-url": "^1.0.10", "form-data": "^4.0.0", "jsonwebtoken": "^8.5.1",