fix(uploads): treat a missing message as an empty one (#7342)

The unsuccessful-upload branch called result.message.trim() directly. A
gateway or proxy that omits message turns a handled upload failure into a
TypeError while handling it, losing the clean "Upload failed" path.

Coerce a non-string message to an empty string before trimming.

The regression test sends success:false without a message and expects
"Upload failed"; it throws a TypeError on main.

Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
This commit is contained in:
Huang-404-Q 2026-10-23 20:03:17 +08:00 committed by GitHub
parent e7d96fe051
commit 4e20cbb4d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 1 deletions

View File

@ -147,7 +147,10 @@ export async function uploadFiles(
const result = (await response.json()) as UploadResponse;
if (result.success === false) {
const message = result.message.trim();
// A gateway or proxy may omit `message`; treat it like an empty one
// instead of throwing a TypeError while handling the failure.
const message =
typeof result.message === "string" ? result.message.trim() : "";
throw new Error(
message ? `Upload incomplete: ${message}` : "Upload failed",
);

View File

@ -94,6 +94,18 @@ describe("uploads api", () => {
expect(error.message).toBe("Upload failed");
});
test("uses the upload fallback when an unsuccessful response omits the message", async () => {
const error = await uploadError(
jsonResponse(200, {
success: false,
files: [],
skipped_files: ["report.pdf"],
}),
);
expect(error.message).toBe("Upload failed");
});
test("preserves string error details", async () => {
const error = await uploadError(
jsonResponse(413, { detail: "File exceeds the upload limit" }),