UNPKG

shora-ai-payment-sdk

Version:

The first open-source payment SDK designed specifically for AI agents and chatbots - ACP Compatible

71 lines (70 loc) 2.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ShoraError = void 0; exports.parseError = parseError; exports.isRetryableError = isRetryableError; exports.getErrorMessage = getErrorMessage; exports.getErrorCode = getErrorCode; exports.getErrorStatus = getErrorStatus; class ShoraError extends Error { constructor(message, status, code, context) { super(message); this.name = 'ShoraError'; this.status = status; this.code = code; this.context = context; this.timestamp = new Date().toISOString(); } } exports.ShoraError = ShoraError; function parseError(error) { if (error instanceof ShoraError) { return error; } if (error.response) { const { status, data } = error.response; const message = data?.message || data?.error || error.message; const code = data?.code || 'API_ERROR'; return new ShoraError(message, status, code, 'API_RESPONSE'); } if (error.request) { return new ShoraError('Network error: No response received', 0, 'NETWORK_ERROR', 'REQUEST'); } return new ShoraError(error.message || 'Unknown error occurred', 0, 'UNKNOWN_ERROR', 'UNKNOWN'); } function isRetryableError(error) { if (error instanceof ShoraError) { return !error.status || error.status >= 500; } return !error.response; } function getErrorMessage(error) { if (error instanceof ShoraError) { return error.message; } if (error.response?.data?.message) { return error.response.data.message; } if (error.message) { return error.message; } return 'An unknown error occurred'; } function getErrorCode(error) { if (error instanceof ShoraError) { return error.code || 'UNKNOWN_ERROR'; } if (error.response?.data?.code) { return error.response.data.code; } return 'UNKNOWN_ERROR'; } function getErrorStatus(error) { if (error instanceof ShoraError) { return error.status; } if (error.response?.status) { return error.response.status; } return undefined; }