Skip to content
Source
Sign inSign up
TanStack/schemas

Quote the provider's reason in shared fetch errors #353

Mergedtombeckenhammerged 2 commits intomain fromfix/352-fetch-error-bodymerged by tombeckenham View on GitHub

tombeckenhamcommented

Closes #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/status docsFailing.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_failed message unique, so group by provider and the text before : to count.
  • The 300-character errorMessage cap 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.ts and replicate.ts. Both failing calls in #352 go through fetchJson, so this is a follow-up.

🤖 Generated with Claude Code

https://claude.ai/code/session_018kKNcVMzBL2gr48hfHTRd2

    1. cloudflare-workers-and-pages[bot](GitHub profile, opens in a new tab)commented

      Deploying with  Cloudflare Workers  Cloudflare Workers

      The latest updates on your project. Learn more about integrating Git with Workers.

      StatusNameLatest CommitPreview URLUpdated (UTC)
      ✅ Deployment successful!
      View logs
      modelschemas5d08c0f1Commit Preview URL

      Branch Preview URL
      Oct 18 2026, 06:50 PM
    2. Triage summary

      QuestionAnswer
      What it solvesAnthropic 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 matterModerately. 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 complexityLow. One private helper with a bounded read (4 KB / 2s, then cancel), about 30 lines.
      Fallbacks that should be errorsNone. The status error always throws. Only the extra snippet is optional: an unreadable or stalled body leaves the old message.
      Public type/API changesNo types change. The error text in PostHog, the admin sync 502 and /v1/status docsFailing.error now carries upstream body text. That last one is public, but it only covers docs loads of public pages.
      Users helped/hurtHelps the operator diagnose upstream failures. Small risk: masked key suffixes, org ids or request ids from upstream bodies can now appear in PostHog.
      Standards complianceFollows CLAUDE.md. No invented values, nothing swallowed, test outside src/routes, no generated artifacts touched. test/lint/typecheck/check all pass.
      VerdictApprove. A small, fail-closed change that makes a recurring failure diagnosable without hiding it.

      Generated by Claude Code

    3. tombeckenham merged thise22a1da

    4. tombeckenham closed this

Sign in to join this conversation.