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) <noreply@anthropic.com>
This commit is contained in:
2026-10-19 00:06:53 +02:00
parent 6e97329a58
commit 6ee9ccc18a

View File

@@ -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<string> {
const text = await res.text().catch(() => '');
if ((res.headers.get('content-type') ?? '').includes('text/html') || /^\s*</.test(text)) return '';
return text.length > 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<never> => {
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}`);
};