fix: handle Anthropic overloaded_error gracefully
When Anthropic's API returns an overloaded_error (temporary capacity issue), the raw JSON error was being sent to users instead of a friendly message. Changes: - Add overloaded error pattern detection - Return user-friendly message for overloaded errors - Classify overloaded as failover-worthy (triggers retry logic)
This commit is contained in:
@@ -392,6 +392,11 @@ export function formatAssistantErrorText(
|
|||||||
return `LLM request rejected: ${invalidRequest[1]}`;
|
return `LLM request rejected: ${invalidRequest[1]}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check for overloaded errors (Anthropic API capacity)
|
||||||
|
if (isOverloadedErrorMessage(raw)) {
|
||||||
|
return "The AI service is temporarily overloaded. Please try again in a moment.";
|
||||||
|
}
|
||||||
|
|
||||||
// Keep it short for WhatsApp.
|
// Keep it short for WhatsApp.
|
||||||
return raw.length > 600 ? `${raw.slice(0, 600)}…` : raw;
|
return raw.length > 600 ? `${raw.slice(0, 600)}…` : raw;
|
||||||
}
|
}
|
||||||
@@ -414,6 +419,7 @@ const ERROR_PATTERNS = {
|
|||||||
"resource_exhausted",
|
"resource_exhausted",
|
||||||
"usage limit",
|
"usage limit",
|
||||||
],
|
],
|
||||||
|
overloaded: [/overloaded_error|"type"\s*:\s*"overloaded"/i, "overloaded"],
|
||||||
timeout: [
|
timeout: [
|
||||||
"timeout",
|
"timeout",
|
||||||
"timed out",
|
"timed out",
|
||||||
@@ -496,6 +502,10 @@ export function isAuthErrorMessage(raw: string): boolean {
|
|||||||
return matchesErrorPatterns(raw, ERROR_PATTERNS.auth);
|
return matchesErrorPatterns(raw, ERROR_PATTERNS.auth);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isOverloadedErrorMessage(raw: string): boolean {
|
||||||
|
return matchesErrorPatterns(raw, ERROR_PATTERNS.overloaded);
|
||||||
|
}
|
||||||
|
|
||||||
export function isCloudCodeAssistFormatError(raw: string): boolean {
|
export function isCloudCodeAssistFormatError(raw: string): boolean {
|
||||||
return matchesErrorPatterns(raw, ERROR_PATTERNS.format);
|
return matchesErrorPatterns(raw, ERROR_PATTERNS.format);
|
||||||
}
|
}
|
||||||
@@ -517,6 +527,7 @@ export type FailoverReason =
|
|||||||
|
|
||||||
export function classifyFailoverReason(raw: string): FailoverReason | null {
|
export function classifyFailoverReason(raw: string): FailoverReason | null {
|
||||||
if (isRateLimitErrorMessage(raw)) return "rate_limit";
|
if (isRateLimitErrorMessage(raw)) return "rate_limit";
|
||||||
|
if (isOverloadedErrorMessage(raw)) return "rate_limit"; // Treat overloaded as rate limit for failover
|
||||||
if (isCloudCodeAssistFormatError(raw)) return "format";
|
if (isCloudCodeAssistFormatError(raw)) return "format";
|
||||||
if (isBillingErrorMessage(raw)) return "billing";
|
if (isBillingErrorMessage(raw)) return "billing";
|
||||||
if (isTimeoutErrorMessage(raw)) return "timeout";
|
if (isTimeoutErrorMessage(raw)) return "timeout";
|
||||||
|
|||||||
Reference in New Issue
Block a user