Add user-friendly error messages for API errors
- Detect credit/quota exhaustion and show helpful message - Handle authentication errors gracefully - Handle model not found errors - Handle content policy violations - Fall back to generic error for unknown issues
This commit is contained in:
@@ -114,7 +114,56 @@ class AIChatCog(commands.Cog):
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Mention response error: {e}", exc_info=True)
|
logger.error(f"Mention response error: {e}", exc_info=True)
|
||||||
await message.reply("Sorry, I encountered an error. Please try again.")
|
error_message = self._get_error_message(e)
|
||||||
|
await message.reply(error_message)
|
||||||
|
|
||||||
|
def _get_error_message(self, error: Exception) -> str:
|
||||||
|
"""Get a user-friendly error message based on the exception type.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
error: The exception that occurred
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A user-friendly error message
|
||||||
|
"""
|
||||||
|
error_str = str(error).lower()
|
||||||
|
|
||||||
|
# Check for credit/quota/billing errors
|
||||||
|
credit_keywords = [
|
||||||
|
"insufficient_quota",
|
||||||
|
"insufficient credits",
|
||||||
|
"quota exceeded",
|
||||||
|
"rate limit",
|
||||||
|
"billing",
|
||||||
|
"payment required",
|
||||||
|
"credit",
|
||||||
|
"exceeded your current quota",
|
||||||
|
"out of credits",
|
||||||
|
"no credits",
|
||||||
|
"balance",
|
||||||
|
"insufficient funds",
|
||||||
|
]
|
||||||
|
|
||||||
|
if any(keyword in error_str for keyword in credit_keywords):
|
||||||
|
return "I'm currently out of API credits. Please try again later or contact the bot administrator."
|
||||||
|
|
||||||
|
# Check for authentication errors
|
||||||
|
auth_keywords = ["invalid api key", "unauthorized", "authentication", "invalid_api_key"]
|
||||||
|
if any(keyword in error_str for keyword in auth_keywords):
|
||||||
|
return (
|
||||||
|
"There's an issue with my API configuration. Please contact the bot administrator."
|
||||||
|
)
|
||||||
|
|
||||||
|
# Check for model errors
|
||||||
|
if "model" in error_str and ("not found" in error_str or "does not exist" in error_str):
|
||||||
|
return "The configured AI model is not available. Please contact the bot administrator."
|
||||||
|
|
||||||
|
# Check for content policy violations
|
||||||
|
if "content policy" in error_str or "safety" in error_str or "blocked" in error_str:
|
||||||
|
return "I can't respond to that request due to content policy restrictions."
|
||||||
|
|
||||||
|
# Default error message
|
||||||
|
return "Sorry, I encountered an error. Please try again."
|
||||||
|
|
||||||
def _extract_message_content(self, message: discord.Message) -> str:
|
def _extract_message_content(self, message: discord.Message) -> str:
|
||||||
"""Extract the actual message content, removing bot mentions."""
|
"""Extract the actual message content, removing bot mentions."""
|
||||||
|
|||||||
Reference in New Issue
Block a user