Skip to main content
Glama
palhamel
by palhamel
errors.ts2.12 kB
/** * Custom error types for the MCP SMS server */ export class SmsError extends Error { constructor(message: string, public code?: string) { super(message); this.name = 'SmsError'; } } export class ValidationError extends SmsError { constructor(message: string) { super(message, 'VALIDATION_ERROR'); this.name = 'ValidationError'; } } export class ElksApiError extends SmsError { constructor(message: string, public statusCode?: number) { super(message, 'ELKS_API_ERROR'); this.name = 'ElksApiError'; } } export class ConfigurationError extends SmsError { constructor(message: string) { super(message, 'CONFIGURATION_ERROR'); this.name = 'ConfigurationError'; } } /** * Format error response for MCP tools */ export const formatErrorResponse = (error: unknown): { type: 'text'; text: string } => { let errorMessage: string; let errorCode: string | undefined; if (error instanceof SmsError) { errorMessage = error.message; errorCode = error.code; } else if (error instanceof Error) { errorMessage = error.message; } else { errorMessage = 'An unknown error occurred'; } const formattedMessage = errorCode ? `Error (${errorCode}): ${errorMessage}` : `Error: ${errorMessage}`; return { type: 'text', text: formattedMessage }; }; /** * Handle and format validation errors */ export const handleValidationError = ( field: string, validation: { isValid: boolean; error?: string } ): void => { if (!validation.isValid && validation.error) { throw new ValidationError(`Invalid ${field}: ${validation.error}`); } }; /** * Wrap async operations with error handling */ export const withErrorHandling = async <T>( operation: () => Promise<T>, context: string ): Promise<T> => { try { return await operation(); } catch (error) { if (error instanceof SmsError) { throw error; // Re-throw our custom errors } // Convert other errors to SmsError const message = error instanceof Error ? error.message : 'Unknown error'; throw new SmsError(`${context}: ${message}`); } };

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/palhamel/46elks-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server