mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 18:58:26 +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:
@@ -59,6 +59,30 @@ describe("runWithModelFallback", () => {
|
||||
expect(run.mock.calls[1]?.[1]).toBe("claude-haiku-3-5");
|
||||
});
|
||||
|
||||
it("falls back on transient HTTP 5xx errors", async () => {
|
||||
const cfg = makeCfg();
|
||||
const run = vi
|
||||
.fn()
|
||||
.mockRejectedValueOnce(
|
||||
new Error(
|
||||
"521 <!DOCTYPE html><html><head><title>Web server is down</title></head><body>Cloudflare</body></html>",
|
||||
),
|
||||
)
|
||||
.mockResolvedValueOnce("ok");
|
||||
|
||||
const result = await runWithModelFallback({
|
||||
cfg,
|
||||
provider: "openai",
|
||||
model: "gpt-4.1-mini",
|
||||
run,
|
||||
});
|
||||
|
||||
expect(result.result).toBe("ok");
|
||||
expect(run).toHaveBeenCalledTimes(2);
|
||||
expect(run.mock.calls[1]?.[0]).toBe("anthropic");
|
||||
expect(run.mock.calls[1]?.[1]).toBe("claude-haiku-3-5");
|
||||
});
|
||||
|
||||
it("falls back on 402 payment required", async () => {
|
||||
const cfg = makeCfg();
|
||||
const run = vi
|
||||
|
||||
@@ -24,6 +24,11 @@ describe("classifyFailoverReason", () => {
|
||||
expect(classifyFailoverReason("invalid request format")).toBe("format");
|
||||
expect(classifyFailoverReason("credit balance too low")).toBe("billing");
|
||||
expect(classifyFailoverReason("deadline exceeded")).toBe("timeout");
|
||||
expect(
|
||||
classifyFailoverReason(
|
||||
"521 <!DOCTYPE html><html><head><title>Web server is down</title></head><body>Cloudflare</body></html>",
|
||||
),
|
||||
).toBe("timeout");
|
||||
expect(classifyFailoverReason("string should match pattern")).toBe("format");
|
||||
expect(classifyFailoverReason("bad request")).toBeNull();
|
||||
expect(
|
||||
|
||||
@@ -22,4 +22,16 @@ describe("formatRawAssistantErrorForUi", () => {
|
||||
"HTTP 500: Internal Server Error",
|
||||
);
|
||||
});
|
||||
|
||||
it("sanitizes HTML error pages into a clean unavailable message", () => {
|
||||
const htmlError = `521 <!DOCTYPE html>
|
||||
<html lang="en-US">
|
||||
<head><title>Web server is down | example.com | Cloudflare</title></head>
|
||||
<body>Ray ID: abc123</body>
|
||||
</html>`;
|
||||
|
||||
expect(formatRawAssistantErrorForUi(htmlError)).toBe(
|
||||
"The AI service is temporarily unavailable (HTTP 521). Please try again in a moment.",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { isCloudflareOrHtmlErrorPage } from "./pi-embedded-helpers.js";
|
||||
|
||||
describe("isCloudflareOrHtmlErrorPage", () => {
|
||||
it("detects Cloudflare 521 HTML pages", () => {
|
||||
const htmlError = `521 <!DOCTYPE html>
|
||||
<html lang="en-US">
|
||||
<head><title>Web server is down | example.com | Cloudflare</title></head>
|
||||
<body><h1>Web server is down</h1></body>
|
||||
</html>`;
|
||||
|
||||
expect(isCloudflareOrHtmlErrorPage(htmlError)).toBe(true);
|
||||
});
|
||||
|
||||
it("detects generic 5xx HTML pages", () => {
|
||||
const htmlError = `503 <html><head><title>Service Unavailable</title></head><body>down</body></html>`;
|
||||
expect(isCloudflareOrHtmlErrorPage(htmlError)).toBe(true);
|
||||
});
|
||||
|
||||
it("does not flag non-HTML status lines", () => {
|
||||
expect(isCloudflareOrHtmlErrorPage("500 Internal Server Error")).toBe(false);
|
||||
expect(isCloudflareOrHtmlErrorPage("429 Too Many Requests")).toBe(false);
|
||||
});
|
||||
|
||||
it("does not flag quoted HTML without a closing html tag", () => {
|
||||
const plainTextWithHtmlPrefix = "500 <!DOCTYPE html> upstream responded with partial HTML text";
|
||||
expect(isCloudflareOrHtmlErrorPage(plainTextWithHtmlPrefix)).toBe(false);
|
||||
});
|
||||
});
|
||||
18
src/agents/pi-embedded-helpers.istransienthttperror.test.ts
Normal file
18
src/agents/pi-embedded-helpers.istransienthttperror.test.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { isTransientHttpError } from "./pi-embedded-helpers.js";
|
||||
|
||||
describe("isTransientHttpError", () => {
|
||||
it("returns true for retryable 5xx status codes", () => {
|
||||
expect(isTransientHttpError("500 Internal Server Error")).toBe(true);
|
||||
expect(isTransientHttpError("502 Bad Gateway")).toBe(true);
|
||||
expect(isTransientHttpError("503 Service Unavailable")).toBe(true);
|
||||
expect(isTransientHttpError("521 <!DOCTYPE html><html></html>")).toBe(true);
|
||||
expect(isTransientHttpError("529 Overloaded")).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false for non-retryable or non-http text", () => {
|
||||
expect(isTransientHttpError("504 Gateway Timeout")).toBe(false);
|
||||
expect(isTransientHttpError("429 Too Many Requests")).toBe(false);
|
||||
expect(isTransientHttpError("network timeout")).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -17,6 +17,7 @@ export {
|
||||
parseApiErrorInfo,
|
||||
sanitizeUserFacingText,
|
||||
isBillingErrorMessage,
|
||||
isCloudflareOrHtmlErrorPage,
|
||||
isCloudCodeAssistFormatError,
|
||||
isCompactionFailureError,
|
||||
isContextOverflowError,
|
||||
@@ -29,6 +30,7 @@ export {
|
||||
isRawApiErrorPayload,
|
||||
isRateLimitAssistantError,
|
||||
isRateLimitErrorMessage,
|
||||
isTransientHttpError,
|
||||
isTimeoutErrorMessage,
|
||||
parseImageDimensionError,
|
||||
parseImageSizeError,
|
||||
|
||||
@@ -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