/** * Standardized Error Handling for autonoma Agents * * Provides consistent error handling, logging, and user-friendly messages * across all agent implementations. */ export interface ErrorMetadata { executionTime: number; errorType: string; agentName: string; timestamp: string; } export interface StandardErrorResponse { error: string; timestamp: string; metadata?: ErrorMetadata; statusCode?: number; } /** * Configuration for agent-specific error messages */ export const AGENT_ERROR_CONFIGS = { 'market-maker': { name: 'Market Maker Agent', defaultMessage: "I'm sorry, I encountered an error while processing your trading request. Please check your connection and try again.", errorPrefix: "Trading Error" }, 'apy': { name: 'DeFi Yield Agent', defaultMessage: "I'm sorry, I encountered an error while processing your DeFi request. Please check your connection and try again.", errorPrefix: "DeFi Error" }, 'portfolio-manager': { name: 'Portfolio Manager Agent', defaultMessage: "I'm sorry, I encountered an error while processing your portfolio request. Please check your connection and try again.", errorPrefix: "Portfolio Error" }, 'memecoin': { name: 'Memecoin Agent', defaultMessage: "I'm sorry, I encountered an error while analyzing memecoins. Please check your connection and try again.", errorPrefix: "Memecoin Error" }, 'orchestrator': { name: 'Agent Orchestrator', defaultMessage: "I'm sorry, I encountered an error while coordinating agents. Please check your connection and try again.", errorPrefix: "Orchestrator Error" } } as const; export type AgentType = keyof typeof AGENT_ERROR_CONFIGS; /** * Standardized error handler for all autonoma agents */ export class AgentErrorHandler { private agentType: AgentType; private startTime: number; constructor(agentType: AgentType, startTime: number = Date.now()) { this.agentType = agentType; this.startTime = startTime; } /** * Handle and format errors with consistent logging and user messages */ handleError(error: unknown, customMessage?: string): StandardErrorResponse { const config = AGENT_ERROR_CONFIGS[this.agentType]; const timestamp = new Date().toISOString(); const executionTime = Date.now() - this.startTime; // Enhanced error logging for debugging const errorDetails = { agent: config.name, message: error instanceof Error ? error.message : 'Unknown error', stack: error instanceof Error ? error.stack : undefined, timestamp, executionTime }; console.error(`${config.errorPrefix}:`, errorDetails); // User-friendly error message const userMessage = customMessage || config.defaultMessage; return { error: userMessage, timestamp, statusCode: 500, metadata: { executionTime, errorType: error instanceof Error ? error.constructor.name : 'UnknownError', agentName: config.name, timestamp } }; } /** * Handle validation errors specifically */ handleValidationError(field: string, issue: string): StandardErrorResponse { const config = AGENT_ERROR_CONFIGS[this.agentType]; const timestamp = new Date().toISOString(); const executionTime = Date.now() - this.startTime; console.error(`${config.errorPrefix} - Validation:`, { field, issue, timestamp }); return { error: `Invalid request: ${field} ${issue}`, timestamp, statusCode: 400, metadata: { executionTime, errorType: 'ValidationError', agentName: config.name, timestamp } }; } /** * Handle service unavailable errors (like MCP servers down) */ handleServiceUnavailable(serviceName: string): StandardErrorResponse { const config = AGENT_ERROR_CONFIGS[this.agentType]; const timestamp = new Date().toISOString(); const executionTime = Date.now() - this.startTime; console.warn(`${config.errorPrefix} - Service Unavailable:`, { serviceName, timestamp }); return { error: `I'm currently unable to access the ${serviceName} service. I can still help with general questions and guidance. Please try again later for advanced features.`, timestamp, statusCode: 503, metadata: { executionTime, errorType: 'ServiceUnavailableError', agentName: config.name, timestamp } }; } } /** * Quick factory function for creating error handlers */ export function createErrorHandler(agentType: AgentType, startTime?: number): AgentErrorHandler { return new AgentErrorHandler(agentType, startTime); } /** * Simple error response for cases where we don't need the full handler */ export function createErrorResponse( agentType: AgentType, error: unknown, customMessage?: string ): StandardErrorResponse { const handler = new AgentErrorHandler(agentType); return handler.handleError(error, customMessage); }