🥅 Add error handling for download queue API
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { Deluge } from '@ctrl/deluge';
|
import { Deluge } from '@ctrl/deluge';
|
||||||
import { AllClientData } from '@ctrl/shared-torrent';
|
import { AllClientData } from '@ctrl/shared-torrent';
|
||||||
|
import Consola from 'consola';
|
||||||
import { getCookie } from 'cookies-next';
|
import { getCookie } from 'cookies-next';
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
import { NextApiRequest, NextApiResponse } from 'next';
|
import { NextApiRequest, NextApiResponse } from 'next';
|
||||||
@@ -18,16 +19,36 @@ const Get = async (request: NextApiRequest, response: NextApiResponse) => {
|
|||||||
const configName = getCookie('config-name', { req: request });
|
const configName = getCookie('config-name', { req: request });
|
||||||
const config = getConfig(configName?.toString() ?? 'default');
|
const config = getConfig(configName?.toString() ?? 'default');
|
||||||
|
|
||||||
const clientData: Promise<NormalizedDownloadAppStat | undefined>[] = config.apps.map((app) =>
|
const failedClients: string[] = [];
|
||||||
GetDataFromClient(app)
|
|
||||||
);
|
const clientData: Promise<NormalizedDownloadAppStat>[] = config.apps.map(async (app) => {
|
||||||
|
try {
|
||||||
|
const response = await GetDataFromClient(app);
|
||||||
|
|
||||||
|
if (!response) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
} as NormalizedDownloadAppStat;
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
} catch (err) {
|
||||||
|
Consola.error(
|
||||||
|
`Error communicating with your download client '${app.name}' (${app.id}): ${err}`
|
||||||
|
);
|
||||||
|
failedClients.push(app.id);
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
} as NormalizedDownloadAppStat;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const settledPromises = await Promise.allSettled(clientData);
|
const settledPromises = await Promise.allSettled(clientData);
|
||||||
|
|
||||||
const data: NormalizedDownloadAppStat[] = settledPromises
|
const data: NormalizedDownloadAppStat[] = settledPromises
|
||||||
.filter((x) => x.status === 'fulfilled')
|
.filter((x) => x.status === 'fulfilled')
|
||||||
.map((promise) => (promise as PromiseFulfilledResult<NormalizedDownloadAppStat>).value)
|
.map((promise) => (promise as PromiseFulfilledResult<NormalizedDownloadAppStat>).value)
|
||||||
.filter((x) => x !== undefined);
|
.filter((x) => x !== undefined && x.type !== undefined);
|
||||||
|
|
||||||
const responseBody = { apps: data } as NormalizedDownloadQueueResponse;
|
const responseBody = { apps: data } as NormalizedDownloadQueueResponse;
|
||||||
|
|
||||||
|
|||||||
@@ -8,16 +8,17 @@ export type NormalizedDownloadQueueResponse = {
|
|||||||
export type NormalizedDownloadAppStat = {
|
export type NormalizedDownloadAppStat = {
|
||||||
success: boolean;
|
success: boolean;
|
||||||
appId: string;
|
appId: string;
|
||||||
totalDownload: number;
|
|
||||||
} & (TorrentTotalDownload | UsenetTotalDownloas);
|
} & (TorrentTotalDownload | UsenetTotalDownloas);
|
||||||
|
|
||||||
export type TorrentTotalDownload = {
|
export type TorrentTotalDownload = {
|
||||||
type: 'torrent';
|
type: 'torrent';
|
||||||
torrents: NormalizedTorrent[];
|
torrents: NormalizedTorrent[];
|
||||||
|
totalDownload: number;
|
||||||
totalUpload: number;
|
totalUpload: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type UsenetTotalDownloas = {
|
export type UsenetTotalDownloas = {
|
||||||
type: 'usenet';
|
type: 'usenet';
|
||||||
|
totalDownload: number;
|
||||||
nzbs: UsenetQueueItem[];
|
nzbs: UsenetQueueItem[];
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user