Skip to main content
Glama
errors.ts2.33 kB
/** * Custom error types for better error handling and debugging */ export class MCPError extends Error { constructor( message: string, public readonly code: string, public readonly details?: Record<string, unknown> ) { super(message); this.name = 'MCPError'; Error.captureStackTrace(this, this.constructor); } } export class ValidationError extends MCPError { constructor(message: string, details?: Record<string, unknown>) { super(message, 'VALIDATION_ERROR', details); this.name = 'ValidationError'; } } export class AuthenticationError extends MCPError { constructor(message: string, details?: Record<string, unknown>) { super(message, 'AUTHENTICATION_ERROR', details); this.name = 'AuthenticationError'; } } export class APIError extends MCPError { constructor( message: string, public readonly statusCode?: number, details?: Record<string, unknown> ) { super(message, 'API_ERROR', details); this.name = 'APIError'; } } export class ConfigurationError extends MCPError { constructor(message: string, details?: Record<string, unknown>) { super(message, 'CONFIGURATION_ERROR', details); this.name = 'ConfigurationError'; } } /** * Format error for MCP response */ export function formatErrorResponse(error: unknown): { message: string; code?: string; details?: Record<string, unknown>; } { if (error instanceof MCPError) { return { message: error.message, code: error.code, details: error.details, }; } if (error instanceof Error) { return { message: error.message, code: 'UNKNOWN_ERROR', }; } return { message: 'An unknown error occurred', code: 'UNKNOWN_ERROR', details: { error: String(error) }, }; } /** * Check if error is a specific type */ export function isValidationError(error: unknown): error is ValidationError { return error instanceof ValidationError; } export function isAuthenticationError(error: unknown): error is AuthenticationError { return error instanceof AuthenticationError; } export function isAPIError(error: unknown): error is APIError { return error instanceof APIError; } export function isConfigurationError(error: unknown): error is ConfigurationError { return error instanceof ConfigurationError; }

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/chenxizhang/m365copilot-mcp'

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