memofai
Version:
Revolutionary dual-track AI memory infrastructure SDK for JavaScript/TypeScript applications
83 lines • 3.08 kB
JavaScript
export class MOAError extends Error {
constructor(message, code, statusCode, response) {
super(message);
this.name = this.constructor.name;
this.code = code;
this.statusCode = statusCode;
this.response = response;
if ('captureStackTrace' in Error) {
Error.captureStackTrace(this, this.constructor);
}
}
}
export class MOAAuthenticationError extends MOAError {
constructor(message = 'Authentication failed. Please check your API key.') {
super(message, 'AUTHENTICATION_ERROR', 401);
}
}
export class MOAValidationError extends MOAError {
constructor(message, validationErrors = []) {
super(message, 'VALIDATION_ERROR', 422);
this.validationErrors = validationErrors;
}
}
export class MOANotFoundError extends MOAError {
constructor(resource, id) {
const message = id ? `${resource} with ID '${id}' not found` : `${resource} not found`;
super(message, 'NOT_FOUND_ERROR', 404);
}
}
export class MOARateLimitError extends MOAError {
constructor(message = 'Rate limit exceeded. Please try again later.', retryAfter) {
super(message, 'RATE_LIMIT_ERROR', 429);
this.retryAfter = retryAfter;
}
}
export class MOAServerError extends MOAError {
constructor(message = 'Internal server error. Please try again later.', statusCode = 500) {
super(message, 'SERVER_ERROR', statusCode);
}
}
export class MOANetworkError extends MOAError {
constructor(message = 'Network error. Please check your connection and try again.') {
super(message, 'NETWORK_ERROR');
}
}
export class MOATimeoutError extends MOAError {
constructor(timeout) {
super(`Request timed out after ${timeout}ms`, 'TIMEOUT_ERROR');
}
}
export class MOAConfigurationError extends MOAError {
constructor(message) {
super(message, 'CONFIGURATION_ERROR');
}
}
export function createErrorFromResponse(status, data, message) {
const defaultMessage = message || 'An error occurred while processing your request';
switch (status) {
case 401:
return new MOAAuthenticationError(message);
case 404:
return new MOANotFoundError('Resource', message);
case 422:
if (data && typeof data === 'object' && 'detail' in data) {
const detail = data.detail;
const validationErrors = detail.map(error => ({
field: error.loc.join('.'),
message: error.msg,
code: error.type,
}));
return new MOAValidationError(defaultMessage, validationErrors);
}
return new MOAValidationError(defaultMessage);
case 429:
return new MOARateLimitError(message);
default:
if (status >= 500) {
return new MOAServerError(message, status);
}
return new MOAError(defaultMessage, 'UNKNOWN_ERROR', status, data);
}
}
//# sourceMappingURL=index.js.map