minimax-mcp-tools
Version:
Async MCP server with Minimax API integration for image generation and text-to-speech
142 lines • 5.36 kB
JavaScript
export class MinimaxError extends Error {
code;
details;
timestamp;
constructor(message, code = 'MINIMAX_ERROR', details = null) {
super(message);
this.name = this.constructor.name;
this.code = code;
this.details = details;
this.timestamp = new Date().toISOString();
}
toJSON() {
return {
name: this.name,
message: this.message,
code: this.code,
details: this.details,
timestamp: this.timestamp
};
}
}
export class MinimaxConfigError extends MinimaxError {
constructor(message, details = null) {
super(message, 'CONFIG_ERROR', details);
}
}
export class MinimaxAPIError extends MinimaxError {
statusCode;
response;
constructor(message, statusCode = null, response = null) {
super(message, 'API_ERROR', { statusCode, response });
this.statusCode = statusCode;
this.response = response;
}
}
export class MinimaxValidationError extends MinimaxError {
field;
value;
constructor(message, field = null, value = null) {
super(message, 'VALIDATION_ERROR', { field, value });
this.field = field;
this.value = value;
}
}
export class MinimaxNetworkError extends MinimaxError {
originalError;
constructor(message, originalError = null) {
super(message, 'NETWORK_ERROR', { originalError: originalError?.message });
this.originalError = originalError;
}
}
export class MinimaxTimeoutError extends MinimaxError {
timeout;
constructor(message, timeout = null) {
super(message, 'TIMEOUT_ERROR', { timeout });
this.timeout = timeout;
}
}
export class MinimaxRateLimitError extends MinimaxError {
retryAfter;
constructor(message, retryAfter = null) {
super(message, 'RATE_LIMIT_ERROR', { retryAfter });
this.retryAfter = retryAfter;
}
}
export class ErrorHandler {
static handleAPIError(error, response) {
if (response?.base_resp && response.base_resp.status_code !== 0) {
const statusCode = response.base_resp.status_code;
const message = response.base_resp.status_msg || 'API request failed';
switch (statusCode) {
case 1004:
return new MinimaxAPIError(`Authentication failed: ${message}`, statusCode, response);
case 1013:
return new MinimaxRateLimitError(`Rate limit exceeded: ${message}`, response.base_resp?.retry_after);
default:
return new MinimaxAPIError(message, statusCode, response);
}
}
if (error.message && error.message.includes('HTTP')) {
const match = error.message.match(/HTTP (\d+):/);
const statusCode = match ? parseInt(match[1], 10) : null;
switch (statusCode) {
case 401:
return new MinimaxAPIError('Unauthorized: Invalid API key', statusCode);
case 403:
return new MinimaxAPIError('Forbidden: Access denied', statusCode);
case 404:
return new MinimaxAPIError('Not found: Invalid endpoint', statusCode);
case 429:
return new MinimaxRateLimitError('Rate limit exceeded', null);
case 500:
return new MinimaxAPIError('Internal server error', statusCode);
default:
return new MinimaxAPIError(error.message, statusCode);
}
}
if (error.code === 'ECONNREFUSED' || error.code === 'ENOTFOUND') {
return new MinimaxNetworkError('Network connection failed', error);
}
if (error.name === 'AbortError' || (error.message && error.message.includes('timeout'))) {
return new MinimaxTimeoutError('Request timeout', error.timeout);
}
return new MinimaxError(error.message || 'Unknown error occurred');
}
static formatErrorForUser(error) {
if (error instanceof MinimaxConfigError) {
return `Configuration Error: ${error.message}`;
}
if (error instanceof MinimaxValidationError) {
return `Validation Error: ${error.message}`;
}
if (error instanceof MinimaxAPIError) {
return `API Error: ${error.message}`;
}
if (error instanceof MinimaxNetworkError) {
return `Network Error: ${error.message}`;
}
if (error instanceof MinimaxTimeoutError) {
return `Timeout Error: ${error.message}`;
}
if (error instanceof MinimaxRateLimitError) {
return `Rate Limit Error: ${error.message}`;
}
return `Error: ${error.message}`;
}
static logError(error, context = {}) {
const logEntry = {
timestamp: new Date().toISOString(),
error: error instanceof MinimaxError ? error.toJSON() : {
name: error.name,
message: error.message,
stack: error.stack
},
context
};
if (typeof console !== 'undefined') {
console.error('[MINIMAX-ERROR]', JSON.stringify(logEntry, null, 2));
}
}
}
//# sourceMappingURL=error-handler.js.map