From cdf8ab18fcc5afd4762a0aa271f938a43ca59fea Mon Sep 17 00:00:00 2001 From: "Gan, Jimmy" Date: Mon, 13 Apr 2026 10:25:40 +0800 Subject: [PATCH] fix: add error handling and feedback for file downloads --- dashboard/frontend/src/lib/api.js | 37 ++++++++++++++-------- dashboard/frontend/src/routes/Files.svelte | 34 ++++++++++++++++++-- 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/dashboard/frontend/src/lib/api.js b/dashboard/frontend/src/lib/api.js index faba7bc..b54f049 100644 --- a/dashboard/frontend/src/lib/api.js +++ b/dashboard/frontend/src/lib/api.js @@ -202,20 +202,29 @@ export async function download(path, filename) { const headers = {}; if (token) headers["Authorization"] = `Bearer ${token}`; - const r = await fetch(`${BASE}/files/download?path=${encodeURIComponent(path)}`, { - method: "GET", - headers, - }); + try { + const r = await fetch(`${BASE}/files/download?path=${encodeURIComponent(path)}`, { + method: "GET", + headers, + }); - if (!r.ok) throw new Error(`HTTP ${r.status}`); + if (!r.ok) { + const errorText = await r.text(); + console.error(`Download failed: ${r.status} - ${errorText}`); + throw new Error(`Download failed: ${r.status}`); + } - const blob = await r.blob(); - const url = window.URL.createObjectURL(blob); - const a = document.createElement("a"); - a.href = url; - a.download = filename; - document.body.appendChild(a); - a.click(); - window.URL.revokeObjectURL(url); - document.body.removeChild(a); + const blob = await r.blob(); + const url = window.URL.createObjectURL(blob); + const a = document.createElement("a"); + a.href = url; + a.download = filename; + document.body.appendChild(a); + a.click(); + window.URL.revokeObjectURL(url); + document.body.removeChild(a); + } catch (e) { + console.error("Download error:", e); + throw e; + } } diff --git a/dashboard/frontend/src/routes/Files.svelte b/dashboard/frontend/src/routes/Files.svelte index f3ddd21..bb575ed 100644 --- a/dashboard/frontend/src/routes/Files.svelte +++ b/dashboard/frontend/src/routes/Files.svelte @@ -6,6 +6,8 @@ let currentPath = $state(""); let loading = $state(true); let uploading = $state(false); + let downloading = $state(false); + let error = $state(""); let fileInput; async function browse(path) { @@ -31,7 +33,16 @@ async function handleDownload(name) { const path = currentPath ? `${currentPath}/${name}` : name; - await download(path, name); + downloading = true; + error = ""; + try { + await download(path, name); + } catch (e) { + error = `Failed to download "${name}": ${e.message}`; + console.error("Download failed:", e); + } finally { + downloading = false; + } } async function handleUpload() { @@ -84,6 +95,23 @@ + + {#if error} +
+ + + +
+

{error}

+
+ +
+ {/if} +
@@ -137,7 +165,9 @@ {e.is_dir ? "—" : formatSize(e.size)}
{#if !e.is_dir} - + {/if}