mirror of
https://github.com/hydralauncher/hydra.git
synced 2026-01-25 20:01:03 +00:00
refactor: streamline error handling in download processes by utilizing a dedicated error handler
This commit is contained in:
@@ -8,8 +8,8 @@ import {
|
||||
gamesSublevel,
|
||||
levelKeys,
|
||||
} from "@main/level";
|
||||
import { Downloader, DownloadError, parseBytes } from "@shared";
|
||||
import { AxiosError } from "axios";
|
||||
import { parseBytes } from "@shared";
|
||||
import { handleDownloadError } from "@main/helpers";
|
||||
|
||||
const addGameToQueue = async (
|
||||
_event: Electron.IpcMainInvokeEvent,
|
||||
@@ -51,56 +51,7 @@ const addGameToQueue = async (
|
||||
await DownloadManager.validateDownloadUrl(download);
|
||||
} catch (err: unknown) {
|
||||
logger.error("Failed to validate download URL for queue", err);
|
||||
|
||||
if (err instanceof AxiosError) {
|
||||
if (err.response?.status === 429 && downloader === Downloader.Gofile) {
|
||||
return { ok: false, error: DownloadError.GofileQuotaExceeded };
|
||||
}
|
||||
|
||||
if (
|
||||
err.response?.status === 403 &&
|
||||
downloader === Downloader.RealDebrid
|
||||
) {
|
||||
return {
|
||||
ok: false,
|
||||
error: DownloadError.RealDebridAccountNotAuthorized,
|
||||
};
|
||||
}
|
||||
|
||||
if (downloader === Downloader.TorBox) {
|
||||
return { ok: false, error: err.response?.data?.detail };
|
||||
}
|
||||
}
|
||||
|
||||
if (err instanceof Error) {
|
||||
if (downloader === Downloader.Buzzheavier) {
|
||||
if (err.message.includes("Rate limit")) {
|
||||
return { ok: false, error: "Buzzheavier: Rate limit exceeded" };
|
||||
}
|
||||
if (
|
||||
err.message.includes("not found") ||
|
||||
err.message.includes("deleted")
|
||||
) {
|
||||
return { ok: false, error: "Buzzheavier: File not found" };
|
||||
}
|
||||
}
|
||||
|
||||
if (downloader === Downloader.FuckingFast) {
|
||||
if (err.message.includes("Rate limit")) {
|
||||
return { ok: false, error: "FuckingFast: Rate limit exceeded" };
|
||||
}
|
||||
if (
|
||||
err.message.includes("not found") ||
|
||||
err.message.includes("deleted")
|
||||
) {
|
||||
return { ok: false, error: "FuckingFast: File not found" };
|
||||
}
|
||||
}
|
||||
|
||||
return { ok: false, error: err.message };
|
||||
}
|
||||
|
||||
return { ok: false };
|
||||
return handleDownloadError(err, downloader);
|
||||
}
|
||||
|
||||
const game = await gamesSublevel.get(gameKey);
|
||||
|
||||
@@ -2,14 +2,14 @@ import { registerEvent } from "../register-event";
|
||||
import type { Download, StartGameDownloadPayload } from "@types";
|
||||
import { DownloadManager, HydraApi, logger } from "@main/services";
|
||||
import { createGame } from "@main/services/library-sync";
|
||||
import { Downloader, DownloadError, parseBytes } from "@shared";
|
||||
import { parseBytes } from "@shared";
|
||||
import {
|
||||
downloadsSublevel,
|
||||
gamesShopAssetsSublevel,
|
||||
gamesSublevel,
|
||||
levelKeys,
|
||||
} from "@main/level";
|
||||
import { AxiosError } from "axios";
|
||||
import { handleDownloadError } from "@main/helpers";
|
||||
|
||||
const startGameDownload = async (
|
||||
_event: Electron.IpcMainInvokeEvent,
|
||||
@@ -102,68 +102,7 @@ const startGameDownload = async (
|
||||
return { ok: true };
|
||||
} catch (err: unknown) {
|
||||
logger.error("Failed to start download", err);
|
||||
|
||||
if (err instanceof AxiosError) {
|
||||
if (err.response?.status === 429 && downloader === Downloader.Gofile) {
|
||||
return { ok: false, error: DownloadError.GofileQuotaExceeded };
|
||||
}
|
||||
|
||||
if (
|
||||
err.response?.status === 403 &&
|
||||
downloader === Downloader.RealDebrid
|
||||
) {
|
||||
return {
|
||||
ok: false,
|
||||
error: DownloadError.RealDebridAccountNotAuthorized,
|
||||
};
|
||||
}
|
||||
|
||||
if (downloader === Downloader.TorBox) {
|
||||
return { ok: false, error: err.response?.data?.detail };
|
||||
}
|
||||
}
|
||||
|
||||
if (err instanceof Error) {
|
||||
if (downloader === Downloader.Buzzheavier) {
|
||||
if (err.message.includes("Rate limit")) {
|
||||
return {
|
||||
ok: false,
|
||||
error: "Buzzheavier: Rate limit exceeded",
|
||||
};
|
||||
}
|
||||
if (
|
||||
err.message.includes("not found") ||
|
||||
err.message.includes("deleted")
|
||||
) {
|
||||
return {
|
||||
ok: false,
|
||||
error: "Buzzheavier: File not found",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (downloader === Downloader.FuckingFast) {
|
||||
if (err.message.includes("Rate limit")) {
|
||||
return {
|
||||
ok: false,
|
||||
error: "FuckingFast: Rate limit exceeded",
|
||||
};
|
||||
}
|
||||
if (
|
||||
err.message.includes("not found") ||
|
||||
err.message.includes("deleted")
|
||||
) {
|
||||
return {
|
||||
ok: false,
|
||||
error: "FuckingFast: File not found",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return { ok: false, error: err.message };
|
||||
}
|
||||
|
||||
return { ok: false };
|
||||
return handleDownloadError(err, downloader);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
51
src/main/helpers/download-error-handler.ts
Normal file
51
src/main/helpers/download-error-handler.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { AxiosError } from "axios";
|
||||
import { Downloader, DownloadError } from "@shared";
|
||||
|
||||
export const handleDownloadError = (
|
||||
err: unknown,
|
||||
downloader: Downloader
|
||||
): { ok: false; error?: string } => {
|
||||
if (err instanceof AxiosError) {
|
||||
if (err.response?.status === 429 && downloader === Downloader.Gofile) {
|
||||
return { ok: false, error: DownloadError.GofileQuotaExceeded };
|
||||
}
|
||||
|
||||
if (err.response?.status === 403 && downloader === Downloader.RealDebrid) {
|
||||
return { ok: false, error: DownloadError.RealDebridAccountNotAuthorized };
|
||||
}
|
||||
|
||||
if (downloader === Downloader.TorBox) {
|
||||
return { ok: false, error: err.response?.data?.detail };
|
||||
}
|
||||
}
|
||||
|
||||
if (err instanceof Error) {
|
||||
if (downloader === Downloader.Buzzheavier) {
|
||||
if (err.message.includes("Rate limit")) {
|
||||
return { ok: false, error: "Buzzheavier: Rate limit exceeded" };
|
||||
}
|
||||
if (
|
||||
err.message.includes("not found") ||
|
||||
err.message.includes("deleted")
|
||||
) {
|
||||
return { ok: false, error: "Buzzheavier: File not found" };
|
||||
}
|
||||
}
|
||||
|
||||
if (downloader === Downloader.FuckingFast) {
|
||||
if (err.message.includes("Rate limit")) {
|
||||
return { ok: false, error: "FuckingFast: Rate limit exceeded" };
|
||||
}
|
||||
if (
|
||||
err.message.includes("not found") ||
|
||||
err.message.includes("deleted")
|
||||
) {
|
||||
return { ok: false, error: "FuckingFast: File not found" };
|
||||
}
|
||||
}
|
||||
|
||||
return { ok: false, error: err.message };
|
||||
}
|
||||
|
||||
return { ok: false };
|
||||
};
|
||||
@@ -94,3 +94,4 @@ export const getThemeSoundPath = (
|
||||
};
|
||||
|
||||
export * from "./reg-parser";
|
||||
export * from "./download-error-handler";
|
||||
|
||||
Reference in New Issue
Block a user