mcp-subagents
Version:
Multi-Agent AI Orchestration via Model Context Protocol - Access specialized CLI AI agents (Aider, Qwen, Gemini, Goose, etc.) with intelligent fallback and configuration
47 lines • 1.88 kB
JavaScript
import { AgentExecutionError } from '../errors.js';
/**
* Type guard to check if an error is an API error response
*/
function isApiErrorResponse(error) {
return (typeof error === 'object' &&
error !== null &&
'status' in error &&
typeof error.status === 'number');
}
/**
* Handles API errors, specifically focusing on 429 Too Many Requests errors
* @param error - The error object from the API call
* @param agentName - The name of the agent making the API call
* @returns A formatted error message
*/
export function handleApiError(error, agentName) {
if (isApiErrorResponse(error)) {
// Handle 429 Too Many Requests error
if (error.status === 429) {
const url = error.request?.responseURL || 'unknown endpoint';
return `Rate limit exceeded for ${agentName} when calling ${url}. Status: ${error.status} - ${error.statusText}`;
}
// Handle other HTTP errors
return `API error for ${agentName}: ${error.status} - ${error.statusText}`;
}
// Handle non-API errors
return `Unknown error for ${agentName}: ${error instanceof Error ? error.message : String(error)}`;
}
/**
* Example function that simulates an API call that might return a 429 error
* @param agentName - The name of the agent making the call
* @returns A promise that might reject with a 429 error
*/
export async function simulateApiCall(agentName) {
// Simulate a 429 error
const errorResponse = {
status: 429,
statusText: 'Too Many Requests',
request: {
responseURL: 'https://cloudcode-pa.googleapis.com/v1internal:streamGenerateContent?alt=sse'
}
};
// Simulate API call failure
throw new AgentExecutionError(handleApiError(errorResponse, agentName), agentName, errorResponse.status);
}
//# sourceMappingURL=api-error-handler.js.map