Fix handling of 413 server response when uploading media (#30737)

This commit is contained in:
Hugh Nimmo-Smith
2025-09-12 11:28:40 +01:00
committed by GitHub
parent 21a86a3269
commit 33d3df24f9
2 changed files with 39 additions and 5 deletions

View File

@@ -63,7 +63,12 @@ import { blobIsAnimated } from "./utils/Image.ts";
const PHYS_HIDPI = [0x00, 0x00, 0x16, 0x25, 0x00, 0x00, 0x16, 0x25, 0x01];
export class UploadCanceledError extends Error {}
export class UploadFailedError extends Error {}
export class UploadFailedError extends Error {
public constructor(cause: any) {
super();
this.cause = cause;
}
}
interface IMediaConfig {
"m.upload.size"?: number;
@@ -367,7 +372,7 @@ export async function uploadFile(
} catch (e) {
if (abortController.signal.aborted) throw new UploadCanceledError();
console.error("Failed to upload file", e);
throw new UploadFailedError();
throw new UploadFailedError(e);
}
if (abortController.signal.aborted) throw new UploadCanceledError();
@@ -386,7 +391,7 @@ export async function uploadFile(
} catch (e) {
if (abortController.signal.aborted) throw new UploadCanceledError();
console.error("Failed to upload file", e);
throw new UploadFailedError();
throw new UploadFailedError(e);
}
if (abortController.signal.aborted) throw new UploadCanceledError();
// If the attachment isn't encrypted then include the URL directly.
@@ -638,15 +643,18 @@ export default class ContentMessages {
dis.dispatch<UploadFinishedPayload>({ action: Action.UploadFinished, upload });
dis.dispatch({ action: "message_sent" });
} catch (error) {
// Unwrap UploadFailedError to get the underlying error
const unwrappedError = error instanceof UploadFailedError && error.cause ? error.cause : error;
// 413: File was too big or upset the server in some way:
// clear the media size limit so we fetch it again next time we try to upload
if (error instanceof HTTPError && error.httpStatus === 413) {
if (unwrappedError instanceof HTTPError && unwrappedError.httpStatus === 413) {
this.mediaConfig = null;
}
if (!upload.cancelled) {
let desc = _t("upload_failed_generic", { fileName: upload.fileName });
if (error instanceof HTTPError && error.httpStatus === 413) {
if (unwrappedError instanceof HTTPError && unwrappedError.httpStatus === 413) {
desc = _t("upload_failed_size", {
fileName: upload.fileName,
});