Skip to main content
Glama
error.util.ts3.31 kB
import { Logger } from './logger.util.js'; /** * Error types for classification */ export enum ErrorType { AUTH_MISSING = 'AUTH_MISSING', AUTH_INVALID = 'AUTH_INVALID', API_ERROR = 'API_ERROR', UNEXPECTED_ERROR = 'UNEXPECTED_ERROR', } /** * Custom error class with type classification */ export class McpError extends Error { type: ErrorType; statusCode?: number; originalError?: unknown; constructor( message: string, type: ErrorType, statusCode?: number, originalError?: unknown, ) { super(message); this.name = 'McpError'; this.type = type; this.statusCode = statusCode; this.originalError = originalError; } } /** * Create an authentication missing error */ export function createAuthMissingError( message: string = 'Authentication credentials are missing', ): McpError { return new McpError(message, ErrorType.AUTH_MISSING); } /** * Create an authentication invalid error */ export function createAuthInvalidError( message: string = 'Authentication credentials are invalid', ): McpError { return new McpError(message, ErrorType.AUTH_INVALID, 401); } /** * Create an API error */ export function createApiError( message: string, statusCode?: number, originalError?: unknown, ): McpError { return new McpError( message, ErrorType.API_ERROR, statusCode, originalError, ); } /** * Create an unexpected error */ export function createUnexpectedError( message: string = 'An unexpected error occurred', originalError?: unknown, ): McpError { return new McpError( message, ErrorType.UNEXPECTED_ERROR, undefined, originalError, ); } /** * Ensure an error is an McpError */ export function ensureMcpError(error: unknown): McpError { if (error instanceof McpError) { return error; } if (error instanceof Error) { return createUnexpectedError(error.message, error); } return createUnexpectedError(String(error)); } /** * Format error for MCP tool response */ export function formatErrorForMcpTool(error: unknown): { content: Array<{ type: 'text'; text: string }>; } { const methodLogger = Logger.forContext( 'utils/error.util.ts', 'formatErrorForMcpTool', ); const mcpError = ensureMcpError(error); methodLogger.error(`${mcpError.type} error`, mcpError); return { content: [ { type: 'text' as const, text: `Error: ${mcpError.message}`, }, ], }; } /** * Format error for MCP resource response */ export function formatErrorForMcpResource( error: unknown, uri: string, ): { contents: Array<{ uri: string; text: string; mimeType: string; description?: string; }>; } { const methodLogger = Logger.forContext( 'utils/error.util.ts', 'formatErrorForMcpResource', ); const mcpError = ensureMcpError(error); methodLogger.error(`${mcpError.type} error`, mcpError); return { contents: [ { uri, text: `Error: ${mcpError.message}`, mimeType: 'text/plain', description: `Error: ${mcpError.type}`, }, ], }; } /** * Handle error in CLI context */ export function handleCliError(error: unknown): never { const methodLogger = Logger.forContext( 'utils/error.util.ts', 'handleCliError', ); const mcpError = ensureMcpError(error); methodLogger.error(`${mcpError.type} error`, mcpError); console.error(`Error: ${mcpError.message}`); process.exit(1); }

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/mrgoonie/reviewwebsite-mcp-server'

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