From 6ee9ccc18ae497d1b6798868de2ed2c89a77eb2a Mon Sep 17 00:00:00 2001 From: Sterister Date: Mon, 19 Oct 2026 00:06:53 +0200 Subject: [PATCH] fix(media): an upload error shows the relay's answer, not an HTML error page When a piece failed through a proxy whose backend was down, the CLI printed Bun's whole HTML error page. Answers that are HTML are now left out of error messages, and the rest is cut at 300 characters. Found in an end-to-end run: two S3+Postgres relays behind a load balancer, `media put` of 300 MiB, one relay killed with SIGKILL mid-upload. The test balancer went strictly round robin with no failover, so every retry's PATCH hit the dead relay and the run stopped, saying to run it again. Run again with failover to the next backend (like nginx proxy_next_upstream), the same command resumed from the first run's job at 31%. Relay 3921 was killed again after 4 pieces, 2 pieces failed over, and relay 3922 took 9. The file came back byte-identical and shade_media_uploads was left empty. Co-Authored-By: Claude Opus 5.5 (1M context) --- packages/shade-media/src/client.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/shade-media/src/client.ts b/packages/shade-media/src/client.ts index 3d98b34..d9ae470 100644 --- a/packages/shade-media/src/client.ts +++ b/packages/shade-media/src/client.ts @@ -34,6 +34,16 @@ export interface PutOptions { onProgress?: (sent: number, total: number) => void; } +/** + * What a refusal says, for an error message: the relay's JSON as it is, but + * not an HTML error page (a proxy's, or Bun's own) poured into a terminal. + */ +async function answerText(res: Response): Promise { + const text = await res.text().catch(() => ''); + if ((res.headers.get('content-type') ?? '').includes('text/html') || /^\s* 300 ? `${text.slice(0, 300)}…` : text; +} + /** Failed tries in a row before a resumable upload gives up. */ const MAX_TRIES = 8; @@ -78,7 +88,7 @@ export class MediaClient { body: body as Blob, }); if (res.status !== 201) { - throw new Error(`upload failed: HTTP ${res.status} ${await res.text().catch(() => '')}`); + throw new Error(`upload failed: HTTP ${res.status} ${await answerText(res)}`); } opts.onProgress?.(size, size); } else { @@ -124,7 +134,7 @@ export class MediaClient { } }; const fail = async (res: Response, what: string): Promise => { - const text = await res.text().catch(() => ''); + const text = await answerText(res); if (res.status >= 500 || res.status === 429) throw new Transient(`${what}: HTTP ${res.status} ${text}`); throw new Error(`upload failed: ${what}: HTTP ${res.status} ${text}`); };