class KillBAPIError extends Error {
constructor(response) {
super(response.message.join(', '));
this.errorCode = response.errorCode;
this.statusCode = response.statusCode;
this.arguments = response.arguments;
}
}
const makeAPIRequest = async (endpoint, options) => {
try {
const response = await fetch(endpoint, options);
if (!response.ok) {
const error = await response.json();
throw new KillBAPIError(error);
}
return await response.json();
} catch (error) {
if (error instanceof KillBAPIError) {
// Handle API errors
console.error(`API Error ${error.errorCode}:`, error.message);
if (error.statusCode === '401') {
// Token expired, refresh
await refreshToken();
return makeAPIRequest(endpoint, options);
}
if (error.errorCode === 'RAMP.0001') {
// Quotation expired, get new one
const newQuote = await getNewQuotation();
// Retry with new quote
}
}
throw error;
}
};