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}`); };