mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 00:08:28 +00:00
(fix): handle Cloudflare 521 and transient 5xx errors gracefully (#13500)
Merged via /review-pr -> /prepare-pr -> /merge-pr.
Prepared head SHA: a8347e95c5
Co-authored-by: rodrigouroz <384037+rodrigouroz@users.noreply.github.com>
Co-authored-by: Takhoffman <781889+Takhoffman@users.noreply.github.com>
Reviewed-by: @Takhoffman
This commit is contained in:
@@ -78,6 +78,10 @@ const ERROR_PREFIX_RE =
|
||||
const CONTEXT_OVERFLOW_ERROR_HEAD_RE =
|
||||
/^(?:context overflow:|request_too_large\b|request size exceeds\b|request exceeds the maximum size\b|context length exceeded\b|maximum context length\b|prompt is too long\b|exceeds model context window\b)/i;
|
||||
const HTTP_STATUS_PREFIX_RE = /^(?:http\s*)?(\d{3})\s+(.+)$/i;
|
||||
const HTTP_STATUS_CODE_PREFIX_RE = /^(?:http\s*)?(\d{3})(?:\s+([\s\S]+))?$/i;
|
||||
const HTML_ERROR_PREFIX_RE = /^\s*(?:<!doctype\s+html\b|<html\b)/i;
|
||||
const CLOUDFLARE_HTML_ERROR_CODES = new Set([521, 522, 523, 524, 525, 526, 530]);
|
||||
const TRANSIENT_HTTP_ERROR_CODES = new Set([500, 502, 503, 521, 522, 523, 524, 529]);
|
||||
const HTTP_ERROR_HINTS = [
|
||||
"error",
|
||||
"bad request",
|
||||
@@ -96,6 +100,50 @@ const HTTP_ERROR_HINTS = [
|
||||
"permission",
|
||||
];
|
||||
|
||||
function extractLeadingHttpStatus(raw: string): { code: number; rest: string } | null {
|
||||
const match = raw.match(HTTP_STATUS_CODE_PREFIX_RE);
|
||||
if (!match) {
|
||||
return null;
|
||||
}
|
||||
const code = Number(match[1]);
|
||||
if (!Number.isFinite(code)) {
|
||||
return null;
|
||||
}
|
||||
return { code, rest: (match[2] ?? "").trim() };
|
||||
}
|
||||
|
||||
export function isCloudflareOrHtmlErrorPage(raw: string): boolean {
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const status = extractLeadingHttpStatus(trimmed);
|
||||
if (!status || status.code < 500) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (CLOUDFLARE_HTML_ERROR_CODES.has(status.code)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return (
|
||||
status.code < 600 && HTML_ERROR_PREFIX_RE.test(status.rest) && /<\/html>/i.test(status.rest)
|
||||
);
|
||||
}
|
||||
|
||||
export function isTransientHttpError(raw: string): boolean {
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed) {
|
||||
return false;
|
||||
}
|
||||
const status = extractLeadingHttpStatus(trimmed);
|
||||
if (!status) {
|
||||
return false;
|
||||
}
|
||||
return TRANSIENT_HTTP_ERROR_CODES.has(status.code);
|
||||
}
|
||||
|
||||
function stripFinalTagsFromText(text: string): string {
|
||||
if (!text) {
|
||||
return text;
|
||||
@@ -133,6 +181,9 @@ function collapseConsecutiveDuplicateBlocks(text: string): string {
|
||||
}
|
||||
|
||||
function isLikelyHttpErrorText(raw: string): boolean {
|
||||
if (isCloudflareOrHtmlErrorPage(raw)) {
|
||||
return true;
|
||||
}
|
||||
const match = raw.match(HTTP_STATUS_PREFIX_RE);
|
||||
if (!match) {
|
||||
return false;
|
||||
@@ -311,6 +362,11 @@ export function formatRawAssistantErrorForUi(raw?: string): string {
|
||||
return "LLM request failed with an unknown error.";
|
||||
}
|
||||
|
||||
const leadingStatus = extractLeadingHttpStatus(trimmed);
|
||||
if (leadingStatus && isCloudflareOrHtmlErrorPage(trimmed)) {
|
||||
return `The AI service is temporarily unavailable (HTTP ${leadingStatus.code}). Please try again in a moment.`;
|
||||
}
|
||||
|
||||
const httpMatch = trimmed.match(HTTP_STATUS_PREFIX_RE);
|
||||
if (httpMatch) {
|
||||
const rest = httpMatch[2].trim();
|
||||
@@ -641,6 +697,10 @@ export function classifyFailoverReason(raw: string): FailoverReason | null {
|
||||
if (isImageSizeError(raw)) {
|
||||
return null;
|
||||
}
|
||||
if (isTransientHttpError(raw)) {
|
||||
// Treat transient 5xx provider failures as retryable transport issues.
|
||||
return "timeout";
|
||||
}
|
||||
if (isRateLimitErrorMessage(raw)) {
|
||||
return "rate_limit";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user