Skip to content

Commit 10d9ec3

Browse files
committed
fix(desktop): fail file upload on non-2xx instead of using the error body as a CID
The file-upload helpers read the daemon response body as the CID without checking the status. On a failed upload the daemon returns a non-2xx with the error text in the body, so that text became the CID and got baked into documents as `ipfs://Failed to add file...`. Guard response.ok in the five upload paths that lacked it (the other five already did).
1 parent c50051b commit 10d9ec3

5 files changed

Lines changed: 22 additions & 1 deletion

File tree

frontend/apps/desktop/src/app-web-importing.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ export async function uploadFile(file: Blob | string) {
2828
method: 'POST',
2929
body: formData,
3030
})
31+
// Guard the status: on failure the daemon returns a non-2xx with the error in
32+
// the body, and returning that text lets it become a `ipfs://<error>` URL.
3133
const data = await response.text()
34+
if (!response.ok) {
35+
throw new Error(`File upload failed (${response.status}): ${data}`)
36+
}
3237
return data
3338
}
3439

frontend/apps/desktop/src/utils/file-upload.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@ export async function fileUpload(file: File) {
99
method: 'POST',
1010
body: formData,
1111
})
12-
return await response.text()
1312
} catch (error: any) {
1413
throw new Error(error)
1514
}
15+
// On failure the daemon returns a non-2xx status with the error in the body.
16+
// Guard the status before returning it: otherwise the error text is used as
17+
// the CID and ends up baked into a document as `ipfs://<error message>`.
18+
const body = await response.text()
19+
if (!response.ok) {
20+
throw new Error(`File upload failed (${response.status}): ${body}`)
21+
}
22+
return body
1623
}

frontend/apps/desktop/src/utils/media-drag.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ export async function handleDragMedia(file: File) {
6363
method: 'POST',
6464
body: formData,
6565
})
66+
if (!response.ok) {
67+
throw new Error(`File upload failed (${response.status}): ${await response.text()}`)
68+
}
6669
const data = await response.text()
6770
return {
6871
url: data ? `ipfs://${data}` : '',

frontend/packages/editor/src/media-container.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ export const MediaContainer = ({
105105
method: 'POST',
106106
body: formData,
107107
})
108+
if (!response.ok) {
109+
throw new Error(`File upload failed (${response.status}): ${await response.text()}`)
110+
}
108111
const data = await response.text()
109112

110113
markBlockUploaded(block.id)

frontend/packages/editor/src/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ export async function handleDragMedia(
238238
method: 'POST',
239239
body: formData,
240240
})
241+
if (!response.ok) {
242+
throw new Error(`File upload failed (${response.status}): ${await response.text()}`)
243+
}
241244
const data = await response.text()
242245
return {
243246
url: data ? `ipfs://${data}` : '',

0 commit comments

Comments
 (0)