From fdfe30e796708bffb7d7e0a6e01cb7422d4a5b0b Mon Sep 17 00:00:00 2001 From: MasterAcnolo Date: Wed, 28 Oct 2026 21:48:47 +0100 Subject: [PATCH] feat(download): update fetch requests to use JSON format and improve error handling --- public/script/downloadstatus.js | 39 ++++++++++++++++++++++++++------- public/script/fetchinfo.js | 19 +++++++++++++--- 2 files changed, 47 insertions(+), 11 deletions(-) diff --git a/public/script/downloadstatus.js b/public/script/downloadstatus.js index c5edf62..edea09b 100644 --- a/public/script/downloadstatus.js +++ b/public/script/downloadstatus.js @@ -53,17 +53,32 @@ form.addEventListener("submit", async (e) => { const formData = new FormData(form); const params = new URLSearchParams(formData); + const body = Object.fromEntries(params.entries()); try { const res = await fetch("/download", { method: "POST", - headers: { "Content-Type": "application/x-www-form-urlencoded" }, - body: params, + headers: {"Content-Type": "application/json"}, + body: JSON.stringify(body), }); if (!res.ok) { if (!userStoppedDownload) { - const errorText = await res.text(); + let errorText = "Server Error"; + try { + const contentType = res.headers.get("content-type") || ""; + if (contentType.includes("application/json")) { + const json = await res.json(); + errorText = + (json && json.error && json.error.message) || + (json && json.message) || + errorText; + } else { + errorText = await res.text(); + } + } catch (e) { + // ignore + } window.showError(errorText); resetProgressBar(); } @@ -71,8 +86,12 @@ form.addEventListener("submit", async (e) => { } // Download successful - check if notifications are enabled - const features = await window.electronAPI.getFeatures(); - if (features.notifySystem) { + try { + const features = await window.electronAPI.getFeatures(); + if (features.notifySystem) { + window.showSuccess("Download completed!"); + } + } catch (e) { window.showSuccess("Download completed!"); } @@ -80,10 +99,14 @@ form.addEventListener("submit", async (e) => { if (!isDownloading && !userStoppedDownload) { statusDiv.style.display = "none"; } + if (!userStoppedDownload) { + window.showError("Network error while starting download"); + resetProgressBar(); + } } finally { isDownloading = false; button.disabled = false; - stopBtn.style.display = "none"; + if (stopBtn) stopBtn.style.display = "none"; } }); @@ -135,9 +158,9 @@ async function stopDownload() { try { await fetch("/download/cancel", { method: "POST", - headers: { "Content-Type": "application/x-www-form-urlencoded" }, + headers: {"Content-Type": "application/json"}, }); - + window.showWarning("Download stopped"); } catch (err) { console.error("Error stopping download:", err); diff --git a/public/script/fetchinfo.js b/public/script/fetchinfo.js index 6893ea1..630becb 100644 --- a/public/script/fetchinfo.js +++ b/public/script/fetchinfo.js @@ -32,14 +32,27 @@ async function fetchVideoInfo(url) { try { const res = await fetch(`/info?url=${encodeURIComponent(url)}`, { method: "GET", - headers: {"Accept": "application/json"} + headers: {Accept: "application/json"}, }); - const data = await res.json(); + let data; + try { + data = await res.json(); + } catch (e) { + return {error: "Network or JSON Issue"}; + } - if (!res.ok) return {error: `An Error occured when fetching info`}; + if (!res.ok) { + if (data && data.error) { + return {error: data.error.message || "An Error occured when fetching info"}; + } + return {error: "An Error occured when fetching info"}; + } if (!data) return { error: "Data is Missing" }; + if (data.error) { + return {error: data.error.message || data.error}; + } return data; } catch (e) {