feat(download): update fetch requests to use JSON format and improve error handling

This commit is contained in:
MasterAcnolo
2026-10-28 21:48:47 +01:00
parent 0699656394
commit fdfe30e796
2 changed files with 47 additions and 11 deletions

View File

@@ -53,17 +53,32 @@ form.addEventListener("submit", async (e) => {
const formData = new FormData(form); const formData = new FormData(form);
const params = new URLSearchParams(formData); const params = new URLSearchParams(formData);
const body = Object.fromEntries(params.entries());
try { try {
const res = await fetch("/download", { const res = await fetch("/download", {
method: "POST", method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" }, headers: {"Content-Type": "application/json"},
body: params, body: JSON.stringify(body),
}); });
if (!res.ok) { if (!res.ok) {
if (!userStoppedDownload) { 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); window.showError(errorText);
resetProgressBar(); resetProgressBar();
} }
@@ -71,8 +86,12 @@ form.addEventListener("submit", async (e) => {
} }
// Download successful - check if notifications are enabled // Download successful - check if notifications are enabled
const features = await window.electronAPI.getFeatures(); try {
if (features.notifySystem) { const features = await window.electronAPI.getFeatures();
if (features.notifySystem) {
window.showSuccess("Download completed!");
}
} catch (e) {
window.showSuccess("Download completed!"); window.showSuccess("Download completed!");
} }
@@ -80,10 +99,14 @@ form.addEventListener("submit", async (e) => {
if (!isDownloading && !userStoppedDownload) { if (!isDownloading && !userStoppedDownload) {
statusDiv.style.display = "none"; statusDiv.style.display = "none";
} }
if (!userStoppedDownload) {
window.showError("Network error while starting download");
resetProgressBar();
}
} finally { } finally {
isDownloading = false; isDownloading = false;
button.disabled = false; button.disabled = false;
stopBtn.style.display = "none"; if (stopBtn) stopBtn.style.display = "none";
} }
}); });
@@ -135,9 +158,9 @@ async function stopDownload() {
try { try {
await fetch("/download/cancel", { await fetch("/download/cancel", {
method: "POST", method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" }, headers: {"Content-Type": "application/json"},
}); });
window.showWarning("Download stopped"); window.showWarning("Download stopped");
} catch (err) { } catch (err) {
console.error("Error stopping download:", err); console.error("Error stopping download:", err);

View File

@@ -32,14 +32,27 @@ async function fetchVideoInfo(url) {
try { try {
const res = await fetch(`/info?url=${encodeURIComponent(url)}`, { const res = await fetch(`/info?url=${encodeURIComponent(url)}`, {
method: "GET", 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) return { error: "Data is Missing" };
if (data.error) {
return {error: data.error.message || data.error};
}
return data; return data;
} catch (e) { } catch (e) {