shora-ai-payment-sdk
Version:
The first open-source payment SDK designed specifically for AI agents and chatbots - ACP Compatible
62 lines (61 loc) • 1.84 kB
JavaScript
export 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();
}
}
export 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');
}
export function isRetryableError(error) {
if (error instanceof ShoraError) {
return !error.status || error.status >= 500;
}
return !error.response;
}
export 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';
}
export 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';
}
export function getErrorStatus(error) {
if (error instanceof ShoraError) {
return error.status;
}
if (error.response?.status) {
return error.response.status;
}
return undefined;
}