Skip to content

Commit 52ddad7

Browse files
committed
fix(api): isRetryableError reads httpStatus + matches credit/quota phrases
1 parent 1fd8322 commit 52ddad7

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

src/api/generateText.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,13 +649,27 @@ function formatPlanForPrompt(plan: Plan): string {
649649
*
650650
* @internal
651651
*/
652+
const RETRYABLE_HTTP_STATUSES = new Set([401, 402, 403, 429, 500, 502, 503, 504]);
653+
652654
export function isRetryableError(error: unknown): boolean {
653655
if (!(error instanceof Error)) return false;
656+
657+
// Typed provider errors carry the HTTP status as a numeric field. Prefer that
658+
// over message-grepping, since providers often substitute the body description
659+
// (e.g. "This request requires more credits...") for the status code.
660+
const status = (error as { httpStatus?: unknown }).httpStatus;
661+
if (typeof status === 'number' && RETRYABLE_HTTP_STATUSES.has(status)) return true;
662+
654663
const msg = error.message;
655-
// HTTP status codes that warrant a provider switch
664+
// HTTP status codes that warrant a provider switch (string-grepped fallback
665+
// when the error type is not a typed provider error).
656666
if (/\b(402|429|500|502|503|504|401|403)\b/.test(msg)) return true;
657667
// Network-level failures
658668
if (/fetch failed|ECONNREFUSED|ETIMEDOUT|ENOTFOUND/i.test(msg)) return true;
669+
// Provider-specific phrases that always imply a retryable condition.
670+
if (/requires more credits|insufficient credits|rate limit|quota|exceeded your current quota/i.test(msg)) {
671+
return true;
672+
}
659673
return false;
660674
}
661675

0 commit comments

Comments
 (0)