@zerokurns/unlayer-ts-client
Version:
TypeScript client library for the Unlayer Cloud API
75 lines • 3.62 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UnlayerApiError = void 0;
exports.isUnlayerApiError = isUnlayerApiError;
exports.handleApiError = handleApiError;
const axios_1 = __importDefault(require("axios"));
/** Custom Error class for Unlayer API errors */
class UnlayerApiError extends Error {
constructor(message, statusCode, responseData, requestData) {
super(message);
this.isUnlayerApiError = true; // Type guard property
this.name = 'UnlayerApiError';
this.statusCode = statusCode;
this.responseData = responseData;
this.requestData = requestData;
// Maintains proper stack trace in V8 environments (Node.js, Chrome) if available
// Check if Error.captureStackTrace exists before calling it
if (typeof Error.captureStackTrace === 'function') {
Error.captureStackTrace(this, UnlayerApiError);
}
}
}
exports.UnlayerApiError = UnlayerApiError;
/**
* Type guard to check if an error is an instance of UnlayerApiError.
* @param error - The error to check.
* @returns True if the error is an UnlayerApiError, false otherwise.
*/
function isUnlayerApiError(error) {
return typeof error === 'object' && error !== null && error.isUnlayerApiError === true;
}
/**
* Parses AxiosError and throws a standardized UnlayerApiError.
* This function guarantees to throw, indicated by the `never` return type.
* @param error - The error caught from an Axios request.
*/
function handleApiError(error) {
var _a, _b;
// Check if it's an AxiosError first
if (axios_1.default.isAxiosError(error)) {
if (error.response) {
// Request made and server responded with a status code outside the 2xx range
const { status, data } = error.response;
// Attempt to get a meaningful message from the response data or fallback
let errorMessage = 'Unknown error';
if (typeof data === 'object' && data !== null && 'message' in data && typeof data.message === 'string') {
errorMessage = data.message;
}
else if (typeof data === 'string' && data) {
errorMessage = data;
}
const message = `Unlayer API Error: Status ${status} - ${errorMessage}`;
throw new UnlayerApiError(message, status, data, (_a = error.config) === null || _a === void 0 ? void 0 : _a.data);
}
else if (error.request) {
// Request was made but no response was received
throw new UnlayerApiError('Unlayer API Error: No response received from server. Check network connectivity and API endpoint.', undefined, undefined, (_b = error.config) === null || _b === void 0 ? void 0 : _b.data);
}
else {
// Something happened in setting up the request that triggered an Error
throw new UnlayerApiError(`Unlayer API Error: Request setup failed - ${error.message}`);
}
}
else {
// If it's not an AxiosError, re-throw it as a generic UnlayerApiError or just re-throw
// Decide on the best strategy: wrap it or let the original error propagate.
// Wrapping provides consistency but might obscure the original error type.
// For now, wrap it for consistency.
throw new UnlayerApiError(`Unlayer API Error: An unexpected error occurred - ${error.message}`);
}
}
//# sourceMappingURL=errors.js.map