Handle LLM API error and convert to APIError. Args: error: The original exception context: Additional context (e.g., module name) fail_fast: Whether to fail immediately (default: True) Returns: APIError in
(
error: Exception,
context: Optional[str] = None,
fail_fast: bool = True
)
| 13 | |
| 14 | @staticmethod |
| 15 | def handle_api_error( |
| 16 | error: Exception, |
| 17 | context: Optional[str] = None, |
| 18 | fail_fast: bool = True |
| 19 | ) -> APIError: |
| 20 | """ |
| 21 | Handle LLM API error and convert to APIError. |
| 22 | |
| 23 | Args: |
| 24 | error: The original exception |
| 25 | context: Additional context (e.g., module name) |
| 26 | fail_fast: Whether to fail immediately (default: True) |
| 27 | |
| 28 | Returns: |
| 29 | APIError instance |
| 30 | """ |
| 31 | error_message = str(error) |
| 32 | |
| 33 | # Detect specific error types |
| 34 | if "429" in error_message or "rate limit" in error_message.lower(): |
| 35 | message = ( |
| 36 | "LLM API rate limit exceeded.\n\n" |
| 37 | "The API returned a 429 error, indicating too many requests.\n\n" |
| 38 | "Troubleshooting:\n" |
| 39 | " 1. Wait a few minutes before retrying\n" |
| 40 | " 2. Check your API quota at your provider's dashboard\n" |
| 41 | " 3. Consider upgrading your API plan\n" |
| 42 | " 4. For large repositories, generate during off-peak hours" |
| 43 | ) |
| 44 | elif "401" in error_message or "authentication" in error_message.lower(): |
| 45 | message = ( |
| 46 | "LLM API authentication failed.\n\n" |
| 47 | "Your API key appears to be invalid or expired.\n\n" |
| 48 | "Troubleshooting:\n" |
| 49 | " 1. Verify your API key: codewiki config show\n" |
| 50 | " 2. Update your API key: codewiki config set --api-key <new-key>\n" |
| 51 | " 3. Check that your API key is active in your provider's dashboard" |
| 52 | ) |
| 53 | elif "timeout" in error_message.lower(): |
| 54 | message = ( |
| 55 | "LLM API request timed out.\n\n" |
| 56 | "The API did not respond within the expected time.\n\n" |
| 57 | "Troubleshooting:\n" |
| 58 | " 1. Check your internet connection\n" |
| 59 | " 2. Verify the API service is operational\n" |
| 60 | " 3. Try again in a few moments\n" |
| 61 | " 4. If the issue persists, contact your API provider" |
| 62 | ) |
| 63 | elif "network" in error_message.lower() or "connection" in error_message.lower(): |
| 64 | message = ( |
| 65 | "Network error while connecting to LLM API.\n\n" |
| 66 | "Could not establish connection to the API.\n\n" |
| 67 | "Troubleshooting:\n" |
| 68 | " 1. Check your internet connection\n" |
| 69 | " 2. Verify the base URL: codewiki config show\n" |
| 70 | " 3. Check if you're behind a proxy or firewall\n" |
| 71 | " 4. Try: curl -I <base-url> to test connectivity" |
| 72 | ) |
no test coverage detected