exception handle

This commit is contained in:
narayana shanubhogh 2021-04-27 13:27:22 +05:30
parent a883815d92
commit dda00548e8
3 changed files with 14 additions and 2 deletions

View file

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

View file

@ -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;

View file

@ -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",