Quote the provider's reason in shared fetch errors #353
main fromfix/352-fetch-error-body·merged by tombeckenham View on GitHubCloses #352
What#
Since 15:00 UTC today, Anthropic (403 Forbidden) and Gemini (400 Bad Request) fail their models poll in the same polls (15:00, 15:45, 16:00, 18:30). The polls in between succeed. The likely cause is that both APIs refuse the region the cron ran from, but that can't be confirmed: fetchJson/fetchText threw only the status line and threw away the response body, which is where both providers say why.
Both helpers now build their error with fetchFailure. It reads at most about 4 KB of the body, waits no more than 2s, then cancels the stream, and adds the first 200 characters (whitespace collapsed, … when cut). If the body is empty, unreadable or stalled, the message ends at the status as it did before. The error still throws from the same places. Nothing is caught or downgraded.
This PR doesn't fix the 403/400 itself. It makes the next one say whether a key or a region was refused, so the follow-up (Worker placement, or a key) can be chosen with evidence.
Test#
src/server/providers/fetch-failure.test.ts covers four cases:
- the stated reason is quoted after the status
- a long HTML body is collapsed and cut at 200 characters
- an empty body leaves the message unchanged
- a stalled body throws after 2s with whatever had arrived
The reason and long-body cases fail on main.
bun run test (2059 passed), lint, typecheck and check are all clean.
Review notes (non-critical, not fixed here)#
Four parallel reviews covered correctness, silent failures, tests and comments. They found nothing critical. I took two of their points:
- The body read is now bounded (4 KB / 2s), so a large or stalled error page can't hold memory or delay the throw.
- The comment no longer states #352's hypothesis as fact.
Left as is:
- Upstream text now reaches telemetry. Body text goes to PostHog
ingest_failed, the admin sync 502, and, for docs loads only, the public/v1/statusdocsFailing.error. Major providers mask keys (e.g.sk-…****abcd), but an org id or request id can now appear in PostHog. No redaction pass was added. Say if you want one. - Counting by message gets noisier. Bodies with request ids make each
ingest_failedmessage unique, so group by provider and the text before:to count. - The 300-character
errorMessagecap can cut the snippet when the URL is long. The Gemini and Anthropic listing URLs are short. - About 8 inline copies of the old throw still drop the body. These include Anthropic's spec fetch (
anthropic.ts:48),minimax-docs.ts,provider-aliases.ts,nvidia*.ts,moonshotai-cn.tsandreplicate.ts. Both failing calls in #352 go throughfetchJson, so this is a follow-up.
🤖 Generated with Claude Code
- 1
Triage summary#
Question Answer What it solves Anthropic 403 and Gemini 400 fail the same models polls, and the error doesn't say why. Shared fetch errors now quote up to 200 characters of the provider's error body, so the next failure names its cause (refused key vs refused region). Does it matter Moderately. Both are core providers. 4 of about 15 polls since 15:00 UTC failed, and each failure skips that provider's poll. Data recovers on the next poll and nothing is nulled. Without the body we can't pick the real fix. Added complexity Low. One private helper with a bounded read (4 KB / 2s, then cancel), about 30 lines. Fallbacks that should be errors None. The status error always throws. Only the extra snippet is optional: an unreadable or stalled body leaves the old message. Public type/API changes No types change. The error text in PostHog, the admin sync 502 and /v1/statusdocsFailing.errornow carries upstream body text. That last one is public, but it only covers docs loads of public pages.Users helped/hurt Helps the operator diagnose upstream failures. Small risk: masked key suffixes, org ids or request ids from upstream bodies can now appear in PostHog. Standards compliance Follows CLAUDE.md. No invented values, nothing swallowed, test outside src/routes, no generated artifacts touched. test/lint/typecheck/check all pass.Verdict Approve. A small, fail-closed change that makes a recurring failure diagnosable without hiding it.
Generated by Claude Code
tombeckenham merged thise22a1da
tombeckenham closed this
Sign in to join this conversation.
Deploying with
Cloudflare Workers#
The latest updates on your project. Learn more about integrating Git with Workers.
View logs
Branch Preview URL