🐛 notes file upload error handling and preview resilience
Browse files at this commitf3306dd5333cedfbe0a713b8dc31f9b80ffbf044Parent: 5a4c1b9- fix
- 🐛 notes file upload error handling and preview resilience
5 changed files +52 -29
- src/api/client.ts +12 -1
- src/components/notes/note-file-preview.tsx +27 -18
- src/components/notes/note-files-panel.tsx +2 -8
- src/i18n/messages.ts +3 -0
- src/pages/notes-page.tsx +8 -2
src/api/client.tsmodified+12 -1
| @@ -209,7 +209,18 @@ export async function blobClient(path: string, signal?: AbortSignal): Promise<Bl | ||
| 209 | 209 | }); |
| 210 | 210 | |
| 211 | 211 | if (!res.ok) { |
| 212 | - throw new ApiError(res.status, res.statusText || "Request failed"); | |
| 212 | + const text = await res.text(); | |
| 213 | + let message = res.statusText || "Request failed"; | |
| 214 | + if (text) { | |
| 215 | + try { | |
| 216 | + const data = JSON.parse(text) as { error?: unknown; message?: unknown }; | |
| 217 | + if (typeof data.error === "string") message = data.error; | |
| 218 | + else if (typeof data.message === "string") message = data.message; | |
| 219 | + } catch { | |
| 220 | + message = text; | |
| 221 | + } | |
| 222 | + } | |
| 223 | + throw new ApiError(res.status, message); | |
| 213 | 224 | } |
| 214 | 225 | |
| 215 | 226 | return res.blob(); |
src/components/notes/note-file-preview.tsxmodified+27 -18
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | import { Show, createEffect, createSignal, onCleanup } from "solid-js"; |
| 2 | +import { formatApiError } from "@/api/client"; | |
| 2 | 3 | import { getNoteFileBlob } from "@/api/getNoteFileBlob"; |
| 3 | 4 | import type { NoteFile } from "@/api/types"; |
| 4 | 5 | import { getNoteFileUrl } from "@/api/getNoteFileUrl"; |
| @@ -18,6 +19,7 @@ export function NoteFilePreview(props: { noteId: string; file: NoteFile | null; | ||
| 18 | 19 | const t = useT(); |
| 19 | 20 | let currentUrl = ""; |
| 20 | 21 | const [previewUrl, setPreviewUrl] = createSignal(""); |
| 22 | + const [previewError, setPreviewError] = createSignal(""); | |
| 21 | 23 | const downloadUrl = () => (props.file ? getNoteFileUrl(props.noteId, props.file.id) : ""); |
| 22 | 24 | const type = () => props.file?.content_type ?? ""; |
| 23 | 25 | |
| @@ -25,16 +27,21 @@ export function NoteFilePreview(props: { noteId: string; file: NoteFile | null; | ||
| 25 | 27 | if (currentUrl) URL.revokeObjectURL(currentUrl); |
| 26 | 28 | currentUrl = ""; |
| 27 | 29 | setPreviewUrl(""); |
| 30 | + setPreviewError(""); | |
| 28 | 31 | |
| 29 | 32 | const file = props.file; |
| 30 | 33 | if (!file || !canPreview(file)) return; |
| 31 | 34 | |
| 32 | 35 | const controller = new AbortController(); |
| 33 | - void getNoteFileBlob(props.noteId, file.id, controller.signal).then((blob) => { | |
| 34 | - if (controller.signal.aborted) return; | |
| 35 | - currentUrl = URL.createObjectURL(blob); | |
| 36 | - setPreviewUrl(currentUrl); | |
| 37 | - }); | |
| 36 | + void getNoteFileBlob(props.noteId, file.id, controller.signal) | |
| 37 | + .then((blob) => { | |
| 38 | + if (controller.signal.aborted) return; | |
| 39 | + currentUrl = URL.createObjectURL(blob); | |
| 40 | + setPreviewUrl(currentUrl); | |
| 41 | + }) | |
| 42 | + .catch((err) => { | |
| 43 | + if (!controller.signal.aborted) setPreviewError(formatApiError(err)); | |
| 44 | + }); | |
| 38 | 45 | |
| 39 | 46 | onCleanup(() => { |
| 40 | 47 | controller.abort(); |
| @@ -69,19 +76,21 @@ export function NoteFilePreview(props: { noteId: string; file: NoteFile | null; | ||
| 69 | 76 | when={canPreview(file())} |
| 70 | 77 | fallback={<p class="mx-auto max-w-md rounded-md border border-dashed border-white/20 bg-white/5 px-4 py-8 text-center text-sm text-white/70">{t("notes.previewUnsupported")}</p>} |
| 71 | 78 | > |
| 72 | - <Show when={type().startsWith("image/") && previewUrl()} fallback={<p class="text-sm text-white/60">{t("common.loading")}</p>}> | |
| 73 | - <img src={previewUrl()} alt={file().name} class="max-h-full max-w-full object-contain" /> | |
| 74 | - </Show> | |
| 75 | - <Show when={type().startsWith("video/") && previewUrl()}> | |
| 76 | - <video src={previewUrl()} controls class="max-h-full max-w-full" /> | |
| 77 | - </Show> | |
| 78 | - <Show when={type().startsWith("audio/") && previewUrl()}> | |
| 79 | - <div class="w-full max-w-2xl rounded-lg bg-background p-4 shadow-sm"> | |
| 80 | - <audio src={previewUrl()} controls class="w-full" /> | |
| 81 | - </div> | |
| 82 | - </Show> | |
| 83 | - <Show when={canFrame(type()) && previewUrl()}> | |
| 84 | - <iframe title={file().name} src={previewUrl()} class="h-full w-full rounded-md border border-white/10 bg-background" /> | |
| 79 | + <Show when={!previewError()} fallback={<p class="mx-auto max-w-md rounded-md border border-dashed border-white/20 bg-white/5 px-4 py-8 text-center text-sm text-white/70">{previewError()}</p>}> | |
| 80 | + <Show when={type().startsWith("image/") && previewUrl()} fallback={<p class="text-sm text-white/60">{t("common.loading")}</p>}> | |
| 81 | + <img src={previewUrl()} alt={file().name} class="max-h-full max-w-full object-contain" /> | |
| 82 | + </Show> | |
| 83 | + <Show when={type().startsWith("video/") && previewUrl()}> | |
| 84 | + <video src={previewUrl()} controls class="max-h-full max-w-full" /> | |
| 85 | + </Show> | |
| 86 | + <Show when={type().startsWith("audio/") && previewUrl()}> | |
| 87 | + <div class="w-full max-w-2xl rounded-lg bg-background p-4 shadow-sm"> | |
| 88 | + <audio src={previewUrl()} controls class="w-full" /> | |
| 89 | + </div> | |
| 90 | + </Show> | |
| 91 | + <Show when={canFrame(type()) && previewUrl()}> | |
| 92 | + <iframe title={file().name} src={previewUrl()} class="h-full w-full rounded-md border border-white/10 bg-background" /> | |
| 93 | + </Show> | |
| 85 | 94 | </Show> |
| 86 | 95 | </Show> |
| 87 | 96 | </div> |
src/components/notes/note-files-panel.tsxmodified+2 -8
| @@ -4,7 +4,7 @@ import { getNoteFileUrl } from "@/api/getNoteFileUrl"; | ||
| 4 | 4 | import { getNoteFiles } from "@/api/getNoteFiles"; |
| 5 | 5 | import { getSettings } from "@/api/getSettings"; |
| 6 | 6 | import { postNoteFile } from "@/api/postNoteFile"; |
| 7 | -import { ApiError, formatApiError } from "@/api/client"; | |
| 7 | +import { formatApiError } from "@/api/client"; | |
| 8 | 8 | import type { NoteFile } from "@/api/types"; |
| 9 | 9 | import { NoteFilePreview } from "@/components/notes/note-file-preview"; |
| 10 | 10 | import { Button } from "@/components/ui/button"; |
| @@ -65,13 +65,7 @@ export function NoteFilesPanel(props: { noteId: string; active: boolean }) { | ||
| 65 | 65 | () => (props.active ? props.noteId : null), |
| 66 | 66 | async (noteId) => { |
| 67 | 67 | if (!noteId) return [] as NoteFile[]; |
| 68 | - // ponytail: empty on 404 so missing BE file API doesn't kill the reader dialog | |
| 69 | - try { | |
| 70 | - return (await getNoteFiles(noteId, { limit: MAX_NOTE_FILES })).items; | |
| 71 | - } catch (err) { | |
| 72 | - if (err instanceof ApiError && (err.status === 404 || err.status === 501)) return []; | |
| 73 | - throw err; | |
| 74 | - } | |
| 68 | + return (await getNoteFiles(noteId, { limit: MAX_NOTE_FILES })).items; | |
| 75 | 69 | }, |
| 76 | 70 | ); |
| 77 | 71 | const [settings] = createResource(async () => { |
src/i18n/messages.tsmodified+3 -0
| @@ -184,6 +184,7 @@ export type MessageKey = | ||
| 184 | 184 | | "notes.noFiles" |
| 185 | 185 | | "notes.fileLimit" |
| 186 | 186 | | "notes.fileTooLarge" |
| 187 | + | "notes.fileUploadPartial" | |
| 187 | 188 | | "notes.previewUnsupported" |
| 188 | 189 | | "notes.unknownFileType" |
| 189 | 190 | | "notes.helpTitle" |
| @@ -710,6 +711,7 @@ const en: Dict = { | ||
| 710 | 711 | "notes.noFiles": "No files attached.", |
| 711 | 712 | "notes.fileLimit": "This note already has 10 files.", |
| 712 | 713 | "notes.fileTooLarge": "File is too large. Max {size}.", |
| 714 | + "notes.fileUploadPartial": "Note saved, but {count} file(s) could not be uploaded.", | |
| 713 | 715 | "notes.previewUnsupported": "Preview is not available for this file type. Download the file to open it.", |
| 714 | 716 | "notes.unknownFileType": "Unknown file type", |
| 715 | 717 | "notes.helpTitle": "About notes", |
| @@ -1248,6 +1250,7 @@ const tr: Dict = { | ||
| 1248 | 1250 | "notes.noFiles": "Henüz dosya eklenmemiş.", |
| 1249 | 1251 | "notes.fileLimit": "Bu notta zaten 10 dosya var.", |
| 1250 | 1252 | "notes.fileTooLarge": "Dosya çok büyük. En fazla {size}.", |
| 1253 | + "notes.fileUploadPartial": "Not kaydedildi ama {count} dosya yüklenemedi.", | |
| 1251 | 1254 | "notes.previewUnsupported": "Bu dosya türü için önizleme yok. Açmak için dosyayı indirin.", |
| 1252 | 1255 | "notes.unknownFileType": "Bilinmeyen dosya türü", |
| 1253 | 1256 | "notes.helpTitle": "Notlar hakkında", |
src/pages/notes-page.tsxmodified+8 -2
| @@ -86,16 +86,22 @@ function NotesContent() { | ||
| 86 | 86 | submitLabel={t("common.create")} |
| 87 | 87 | onCancel={() => setCreateOpen(false)} |
| 88 | 88 | onSubmit={async (values) => { |
| 89 | + setError(""); | |
| 89 | 90 | const note = await postNote({ |
| 90 | 91 | title: values.title, |
| 91 | 92 | content: values.content || undefined, |
| 92 | 93 | }); |
| 93 | - // ponytail: upload after create — file API needs note id | |
| 94 | + let failedUploads = 0; | |
| 94 | 95 | for (const file of values.files) { |
| 95 | - await postNoteFile(note.id, file); | |
| 96 | + try { | |
| 97 | + await postNoteFile(note.id, file); | |
| 98 | + } catch { | |
| 99 | + failedUploads += 1; | |
| 100 | + } | |
| 96 | 101 | } |
| 97 | 102 | await refetch(); |
| 98 | 103 | setCreateOpen(false); |
| 104 | + if (failedUploads > 0) setError(t("notes.fileUploadPartial", { count: failedUploads })); | |
| 99 | 105 | }} |
| 100 | 106 | /> |
| 101 | 107 | </SidePanel> |