/**
* errorHandling.ts
* Centralized error handling utilities for consistent error responses
*/
import type { CallToolResult } from '@modelcontextprotocol/sdk/types.js';
import { ValidationError } from '../validation/schemas.js';
/**
* Creates a descriptive error message, showing validation details in dev mode.
*/
function createErrorMessage(operation: string, error: unknown): string {
const message =
error instanceof Error ? error.message : 'System error occurred';
const isDev = process.env.NODE_ENV === 'development' || process.env.DEBUG;
// For validation errors, always return the detailed message.
if (error instanceof ValidationError) {
return message;
}
// For other errors, be generic in production.
return isDev
? `Failed to ${operation}: ${message}`
: `Failed to ${operation}: System error occurred`;
}
/**
* Utility for handling async operations with consistent error handling
*/
export async function handleAsyncOperation(
operation: () => Promise<string>,
operationName: string,
): Promise<CallToolResult> {
try {
const result = await operation();
return {
content: [{ type: 'text', text: result }],
isError: false,
};
} catch (error) {
return {
content: [
{
type: 'text',
text: createErrorMessage(operationName, error),
},
],
isError: true,
};
}
}