@infosel-sdk/core
Version:
Core SDK for Infosel financial services platform. Provides essential infrastructure for authentication, HTTP/GraphQL communication, storage management, and error handling.
91 lines • 4.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseSdkError = exports.parseAxiosError = void 0;
const tslib_1 = require("tslib");
const client_1 = require("@apollo/client");
const axios_1 = require("axios");
const sdk_error_1 = tslib_1.__importStar(require("./sdk_error"));
const instanceOfError = (error) => {
if (error && typeof error === 'object') {
return 'name' in error && 'message' in error && 'stack' in error;
}
return false;
};
const instanceOfAxiosError = (error) => {
if (error && typeof error === 'object') {
return (0, axios_1.isAxiosError)(error);
}
return false;
};
const instanceOfApolloError = (error) => {
if (error && typeof error === 'object' && error instanceof client_1.ApolloError) {
return (0, client_1.isApolloError)(error);
}
return false;
};
const instanceOfSdkError = (error) => {
return error instanceof sdk_error_1.default;
};
const parseAxiosError = (axiosError) => {
var _a, _b, _c, _d, _e, _f, _g, _h;
try {
if (axiosError && axiosError.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
const errorMessage =
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
((_a = axiosError.response) === null || _a === void 0 ? void 0 : _a.message) ||
(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
(_c = (_b = axiosError.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.message) ||
(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
(_f = (_e = (_d = axiosError.response) === null || _d === void 0 ? void 0 : _d.data) === null || _e === void 0 ? void 0 : _e.error) === null || _f === void 0 ? void 0 : _f.message) ||
(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
(_h = (_g = axiosError.response) === null || _g === void 0 ? void 0 : _g.data) === null || _h === void 0 ? void 0 : _h.exp);
return new sdk_error_1.default(sdk_error_1.SdkErrorType.AXIOS_RESPONSE_ERROR, errorMessage);
}
else if (axiosError && axiosError.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
return new sdk_error_1.default(sdk_error_1.SdkErrorType.AXIOS_REQUEST_ERROR, 'The request was made but no response was received.');
}
else if (axiosError) {
// Something happened in setting up the request that triggered an Error
return new sdk_error_1.default(sdk_error_1.SdkErrorType.AXIOS_REQUEST_CANCELED, 'Something happened in setting up the request that triggered an Error.');
}
return new sdk_error_1.default(sdk_error_1.SdkErrorType.UNKNOWN_ERROR, 'Unable to process axios error');
}
catch (error) {
return new sdk_error_1.default(sdk_error_1.SdkErrorType.UNKNOWN_ERROR, JSON.stringify(error));
}
};
exports.parseAxiosError = parseAxiosError;
const parseSdkError = (error) => {
if (instanceOfAxiosError(error)) {
return (0, exports.parseAxiosError)(error);
}
if (instanceOfApolloError(error)) {
const { graphQLErrors, networkError } = error;
const message = graphQLErrors
.map(graphError => graphError.message)
.concat((networkError === null || networkError === void 0 ? void 0 : networkError.message) || '')
.join(', ');
return new sdk_error_1.default(sdk_error_1.SdkErrorType.GRAPH_QL_ERROR, message);
}
if (instanceOfSdkError(error)) {
return error;
}
if (instanceOfError(error)) {
return new sdk_error_1.default(sdk_error_1.SdkErrorType.UNKNOWN_ERROR, error.message);
}
return new sdk_error_1.default(sdk_error_1.SdkErrorType.UNKNOWN_ERROR);
};
exports.parseSdkError = parseSdkError;
//# sourceMappingURL=index.js.map