Code Review MCP Server

by crazyrabbitLTC
Verified
/** * @file Error Handling * @version 0.1.0 * * Error handling utilities for LLM integration */ /** * Error class for API errors that may be retryable */ export class RetryableAPIError extends Error { /** * Whether the error is retryable */ retryable; /** * Constructs a new retryable API error * @param message The error message * @param retryable Whether the error is retryable */ constructor(message, retryable = true) { super(message); this.name = 'RetryableAPIError'; this.retryable = retryable; } } /** * Calls a function with retry logic * @param fn The function to call * @param maxRetries Maximum number of retries * @param initialDelay Initial delay in milliseconds * @returns The result of the function call */ export async function callWithRetry(fn, maxRetries = 3, initialDelay = 1000) { let retries = 0; let delay = initialDelay; while (true) { try { return await fn(); } catch (error) { if (error instanceof RetryableAPIError && error.retryable && retries < maxRetries) { retries++; console.warn(`Retry ${retries}/${maxRetries} after error: ${error.message}`); // Wait before retrying with exponential backoff await new Promise(resolve => setTimeout(resolve, delay)); delay *= 2; // Exponential backoff continue; } // Not retryable or max retries reached throw error; } } }