expresscheckout-nodejs
Version:
Juspay's official expresscheckout-nodejs sdk
182 lines • 6.67 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.InternalServerError = exports.InvalidRequestError = exports.AuthorizationError = exports.AuthenticationError = exports.TimeoutError = exports.APIError = exports.JuspayError = void 0;
const Utils = __importStar(require("./utils.js"));
class JuspayError extends Error {
/**
*
* @param error raw json error response from juspay's api or custom error message, so it's response + message. Default error is Unkown Error
* @param name name of the error optional, default JuspayError
* @returns
*/
constructor(error, http) {
super();
this.name = 'JuspayError';
this.httpInfo = http;
Object.setPrototypeOf(this, JuspayError.prototype);
const defaultErrorMessage = 'Something went wrong, Please check the logs, stacks and messages.';
if (error instanceof Error) {
// for JuspayCryptoError or unexpected error Eg: JSON parser error, Error any other unexpected error
this.message = error.message;
this.name = error.name;
return;
}
this.raw = error;
let payload;
if (typeof error == 'string') {
try {
// parse juspay errors for stringified case
this.message = error;
payload = JSON.parse(error);
}
catch (ignored) {
// actual string, TimeoutError('Connection timeout!!') and Something went wrong cases
this.message = error;
this.raw = error;
return;
}
}
if (typeof error == 'object') {
payload = error;
}
else {
// internal error
this.message = new String(error).toString() || defaultErrorMessage;
return;
}
if (payload == undefined) {
return;
}
this.status = this.parseAsString('status', payload);
this.error_message = this.parseAsString('error_message', payload);
this.user_message = this.parseAsString('user_message', payload);
this.status = this.parseAsString('status', payload);
this.error_code = this.parseAsString('error_code', payload);
this.error_info = this.parseAsObject(payload);
if (this.error_info && this.error_info.fields) {
this.error_info.fields = this.error_info.fields;
}
if (this.error_message == undefined) {
try {
this.message = JSON.stringify(payload);
}
catch (ignored) {
this.message = defaultErrorMessage;
}
}
else {
if (Utils.isDevEnvironment()) {
this.message = `${this.error_message}${this.error_info == undefined
? ''
: `, ${this.error_info.developer_message}`}`;
}
else {
this.message = this.error_message;
}
}
}
parseAsObject(payload) {
const camelCase = this.snakeToCamel('error_info');
const value = payload['error_info'] || payload[camelCase];
if (typeof value == 'object') {
return JSON.parse(JSON.stringify(value));
}
else if (typeof value == 'string') {
try {
return JSON.parse(value);
}
catch (ignored) {
return undefined;
}
}
else {
return undefined;
}
}
parseAsString(snake_case_string, payload) {
const camelCase = this.snakeToCamel(snake_case_string);
const value = payload[snake_case_string] || payload[camelCase];
if (typeof value == 'string') {
return value;
}
return undefined;
}
snakeToCamel(str) {
return str.replace(/_([a-z])/g, function (match, group) {
return group.toUpperCase();
});
}
}
exports.JuspayError = JuspayError;
class APIError extends JuspayError {
constructor(response) {
super(response);
Object.setPrototypeOf(this, APIError.prototype);
this.name = 'ApiError';
}
}
exports.APIError = APIError;
class TimeoutError extends APIError {
constructor(message) {
super(message || 'Error timeout');
Object.setPrototypeOf(this, TimeoutError.prototype);
this.name = 'TimeoutError';
}
}
exports.TimeoutError = TimeoutError;
class AuthenticationError extends APIError {
constructor(error) {
super(error);
Object.setPrototypeOf(this, AuthenticationError.prototype);
this.name = 'AuthenticationError';
}
}
exports.AuthenticationError = AuthenticationError;
class AuthorizationError extends APIError {
constructor(error) {
super(error);
Object.setPrototypeOf(this, AuthorizationError.prototype);
this.name = 'AuthorizationError';
}
}
exports.AuthorizationError = AuthorizationError;
class InvalidRequestError extends APIError {
constructor(error) {
super(error);
Object.setPrototypeOf(this, InvalidRequestError.prototype);
this.name = 'InvalidRequestError';
}
}
exports.InvalidRequestError = InvalidRequestError;
class InternalServerError extends APIError {
constructor(error) {
super(error || 'Something went wrong.');
Object.setPrototypeOf(this, InternalServerError.prototype);
this.name = 'InternalServerError';
}
}
exports.InternalServerError = InternalServerError;
//# sourceMappingURL=JuspayError.js.map