MCPcopy Create free account
hub / github.com/FSoft-AI4Code/CodeWiki / APIErrorHandler

Class APIErrorHandler

codewiki/cli/utils/api_errors.py:11–111  ·  view source on GitHub ↗

Handler for LLM API errors with fail-fast behavior.

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected