mirror of
https://github.com/hydralauncher/hydra.git
synced 2026-01-29 22:01:03 +00:00
refactor: improve error handling in download processes and simplify button content rendering in download settings modal
This commit is contained in:
@@ -57,7 +57,7 @@ const addGameToQueue = async (
|
|||||||
const updatedGame = await gamesSublevel.get(gameKey);
|
const updatedGame = await gamesSublevel.get(gameKey);
|
||||||
|
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
createGame(updatedGame!).catch(() => {}),
|
createGame(updatedGame).catch(() => {}),
|
||||||
HydraApi.post(`/games/${shop}/${objectId}/download`, null, {
|
HydraApi.post(`/games/${shop}/${objectId}/download`, null, {
|
||||||
needsAuth: false,
|
needsAuth: false,
|
||||||
}).catch(() => {}),
|
}).catch(() => {}),
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ const updateDownloadQueuePosition = async (
|
|||||||
|
|
||||||
const download = await downloadsSublevel.get(gameKey);
|
const download = await downloadsSublevel.get(gameKey);
|
||||||
|
|
||||||
if (!download || !download.queued || download.status !== "paused") {
|
if (!download?.queued || download.status !== "paused") {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,48 +1,63 @@
|
|||||||
import { AxiosError } from "axios";
|
import { AxiosError } from "axios";
|
||||||
import { Downloader, DownloadError } from "@shared";
|
import { Downloader, DownloadError } from "@shared";
|
||||||
|
|
||||||
|
type DownloadErrorResult = { ok: false; error?: string };
|
||||||
|
|
||||||
|
const handleAxiosError = (
|
||||||
|
err: AxiosError,
|
||||||
|
downloader: Downloader
|
||||||
|
): DownloadErrorResult | null => {
|
||||||
|
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) {
|
||||||
|
const data = err.response?.data as { detail?: string } | undefined;
|
||||||
|
return { ok: false, error: data?.detail };
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
const HOST_NAMES: Partial<Record<Downloader, string>> = {
|
||||||
|
[Downloader.Buzzheavier]: "Buzzheavier",
|
||||||
|
[Downloader.FuckingFast]: "FuckingFast",
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleHostSpecificError = (
|
||||||
|
message: string,
|
||||||
|
downloader: Downloader
|
||||||
|
): DownloadErrorResult | null => {
|
||||||
|
const hostName = HOST_NAMES[downloader];
|
||||||
|
if (!hostName) return null;
|
||||||
|
|
||||||
|
if (message.includes("Rate limit")) {
|
||||||
|
return { ok: false, error: `${hostName}: Rate limit exceeded` };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (message.includes("not found") || message.includes("deleted")) {
|
||||||
|
return { ok: false, error: `${hostName}: File not found` };
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
export const handleDownloadError = (
|
export const handleDownloadError = (
|
||||||
err: unknown,
|
err: unknown,
|
||||||
downloader: Downloader
|
downloader: Downloader
|
||||||
): { ok: false; error?: string } => {
|
): DownloadErrorResult => {
|
||||||
if (err instanceof AxiosError) {
|
if (err instanceof AxiosError) {
|
||||||
if (err.response?.status === 429 && downloader === Downloader.Gofile) {
|
const result = handleAxiosError(err, downloader);
|
||||||
return { ok: false, error: DownloadError.GofileQuotaExceeded };
|
if (result) return result;
|
||||||
}
|
|
||||||
|
|
||||||
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 (err instanceof Error) {
|
||||||
if (downloader === Downloader.Buzzheavier) {
|
const hostResult = handleHostSpecificError(err.message, downloader);
|
||||||
if (err.message.includes("Rate limit")) {
|
if (hostResult) return hostResult;
|
||||||
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, error: err.message };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -221,6 +221,33 @@ export function DownloadSettingsModal({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getButtonContent = () => {
|
||||||
|
if (downloadStarting) {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<SyncIcon className="download-settings-modal__loading-spinner" />
|
||||||
|
{t("loading")}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasActiveDownload) {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<PlusIcon />
|
||||||
|
{t("add_to_queue")}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<DownloadIcon />
|
||||||
|
{t("download_now")}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
const handleStartClick = async () => {
|
const handleStartClick = async () => {
|
||||||
if (repack) {
|
if (repack) {
|
||||||
setDownloadStarting(true);
|
setDownloadStarting(true);
|
||||||
@@ -462,22 +489,7 @@ export function DownloadSettingsModal({
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
{downloadStarting ? (
|
{getButtonContent()}
|
||||||
<>
|
|
||||||
<SyncIcon className="download-settings-modal__loading-spinner" />
|
|
||||||
{t("loading")}
|
|
||||||
</>
|
|
||||||
) : hasActiveDownload ? (
|
|
||||||
<>
|
|
||||||
<PlusIcon />
|
|
||||||
{t("add_to_queue")}
|
|
||||||
</>
|
|
||||||
) : (
|
|
||||||
<>
|
|
||||||
<DownloadIcon />
|
|
||||||
{t("download_now")}
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user