@bratcliffe909/mcp-server-segmind
Version:
Model Context Protocol server for Segmind API - Generate images and videos using AI models
136 lines • 4.99 kB
JavaScript
export var ErrorCode;
(function (ErrorCode) {
ErrorCode["INVALID_API_KEY"] = "INVALID_API_KEY";
ErrorCode["AUTHENTICATION_FAILED"] = "AUTHENTICATION_FAILED";
ErrorCode["API_ERROR"] = "API_ERROR";
ErrorCode["RATE_LIMIT_EXCEEDED"] = "RATE_LIMIT_EXCEEDED";
ErrorCode["INSUFFICIENT_CREDITS"] = "INSUFFICIENT_CREDITS";
ErrorCode["MODEL_NOT_FOUND"] = "MODEL_NOT_FOUND";
ErrorCode["MODEL_NOT_AVAILABLE"] = "MODEL_NOT_AVAILABLE";
ErrorCode["GENERATION_FAILED"] = "GENERATION_FAILED";
ErrorCode["GENERATION_TIMEOUT"] = "GENERATION_TIMEOUT";
ErrorCode["INVALID_INPUT"] = "INVALID_INPUT";
ErrorCode["INVALID_IMAGE_FORMAT"] = "INVALID_IMAGE_FORMAT";
ErrorCode["IMAGE_TOO_LARGE"] = "IMAGE_TOO_LARGE";
ErrorCode["INTERNAL_ERROR"] = "INTERNAL_ERROR";
ErrorCode["CONFIGURATION_ERROR"] = "CONFIGURATION_ERROR";
ErrorCode["NETWORK_ERROR"] = "NETWORK_ERROR";
ErrorCode["TIMEOUT_ERROR"] = "TIMEOUT_ERROR";
})(ErrorCode || (ErrorCode = {}));
export class SafeError extends Error {
userMessage;
code;
statusCode;
details;
constructor(userMessage, code, statusCode = 500, details) {
super(userMessage);
this.userMessage = userMessage;
this.code = code;
this.statusCode = statusCode;
this.details = details;
this.name = this.constructor.name;
Error.captureStackTrace(this, this.constructor);
}
toJSON() {
return {
error: this.userMessage,
code: this.code,
statusCode: this.statusCode,
...(process.env.NODE_ENV === 'development' && this.details ? { details: this.details } : {}),
};
}
}
export class AuthenticationError extends SafeError {
constructor(message = 'Authentication failed', details) {
super(message, ErrorCode.AUTHENTICATION_FAILED, 401, details);
}
}
export class InvalidApiKeyError extends SafeError {
constructor(message = 'Invalid or missing API key', details) {
super(message, ErrorCode.INVALID_API_KEY, 401, details);
}
}
export class RateLimitError extends SafeError {
constructor(message = 'Rate limit exceeded. Please try again later.', details) {
super(message, ErrorCode.RATE_LIMIT_EXCEEDED, 429, details);
}
}
export class InsufficientCreditsError extends SafeError {
constructor(message = 'Insufficient credits for this operation', details) {
super(message, ErrorCode.INSUFFICIENT_CREDITS, 402, details);
}
}
export class ModelNotFoundError extends SafeError {
constructor(modelId, details) {
super(`Model '${modelId}' not found`, ErrorCode.MODEL_NOT_FOUND, 404, details);
}
}
export class GenerationError extends SafeError {
constructor(message = 'Failed to generate content', details) {
super(message, ErrorCode.GENERATION_FAILED, 500, details);
}
}
export class InvalidInputError extends SafeError {
constructor(message, details) {
super(message, ErrorCode.INVALID_INPUT, 400, details);
}
}
export class NetworkError extends SafeError {
constructor(message = 'Network error occurred', details) {
super(message, ErrorCode.NETWORK_ERROR, 503, details);
}
}
export class TimeoutError extends SafeError {
constructor(message = 'Request timed out', details) {
super(message, ErrorCode.TIMEOUT_ERROR, 504, details);
}
}
export class ConfigurationError extends SafeError {
constructor(message, details) {
super(message, ErrorCode.CONFIGURATION_ERROR, 500, details);
}
}
export function mapToSafeError(error) {
if (error instanceof SafeError) {
return error;
}
if (error instanceof Error) {
const message = error.message.toLowerCase();
if (message.includes('api key') || message.includes('authorization')) {
return new AuthenticationError();
}
if (message.includes('rate limit')) {
return new RateLimitError();
}
if (message.includes('credits')) {
return new InsufficientCreditsError();
}
if (message.includes('timeout')) {
return new TimeoutError();
}
if (message.includes('network') || message.includes('fetch')) {
return new NetworkError();
}
if (process.env.NODE_ENV === 'development' && process.env.MCP_MODE !== 'true') {
console.error('Original error:', error);
}
}
let errorMessage = 'An error occurred processing your request';
if (error instanceof Error) {
errorMessage = error.message || errorMessage;
}
else if (typeof error === 'string') {
errorMessage = error;
}
return new SafeError(errorMessage, ErrorCode.INTERNAL_ERROR, 500);
}
export function formatErrorResponse(error) {
return {
error: {
code: error.code,
message: error.userMessage,
data: error.details,
},
};
}
//# sourceMappingURL=errors.js.map