🐛 Add error catching for calender api route

This commit is contained in:
Manuel Ruwe
2023-01-10 20:55:35 +01:00
parent 4513aa66f0
commit bbf1255b8b
4 changed files with 49 additions and 36 deletions

View File

@@ -3,6 +3,7 @@
"name": "Calendar", "name": "Calendar",
"description": "A calendar module for displaying upcoming releases. It interacts with the Sonarr and Radarr API.", "description": "A calendar module for displaying upcoming releases. It interacts with the Sonarr and Radarr API.",
"settings": { "settings": {
"title": "Settings for calendar integration",
"sundayStart": { "sundayStart": {
"label": "Start the week on Sunday" "label": "Start the week on Sunday"
} }

View File

@@ -26,6 +26,5 @@
} }
}, },
"tip": "You can select the search bar with the shortcut ", "tip": "You can select the search bar with the shortcut ",
"switchedSearchEngine": "Switched to searching with {{searchEngine}}", "switchedSearchEngine": "Switched to searching with {{searchEngine}}"
"configurationName": "Search engine configuration"
} }

View File

@@ -1,5 +1,6 @@
{ {
"title": "Search engine", "title": "Search engine",
"configurationName": "Search engine configuration",
"tips": { "tips": {
"generalTip": "Use the prefixes !yt and !t in front of your query to search on YouTube or for a Torrent respectively.", "generalTip": "Use the prefixes !yt and !t in front of your query to search on YouTube or for a Torrent respectively.",
"placeholderTip": "%s can be used as a placeholder for the query." "placeholderTip": "%s can be used as a placeholder for the query."

View File

@@ -1,4 +1,5 @@
import axios from 'axios'; import axios from 'axios';
import Consola from 'consola';
import { NextApiRequest, NextApiResponse } from 'next'; import { NextApiRequest, NextApiResponse } from 'next';
import { getConfig } from '../../../tools/config/getConfig'; import { getConfig } from '../../../tools/config/getConfig';
import { AppIntegrationType } from '../../../types/app'; import { AppIntegrationType } from '../../../types/app';
@@ -44,44 +45,55 @@ async function Get(req: NextApiRequest, res: NextApiResponse) {
(app) => app.integration && mediaAppIntegrationTypes.includes(app.integration.type) (app) => app.integration && mediaAppIntegrationTypes.includes(app.integration.type)
); );
const medias = await Promise.all( try {
await mediaApps.map(async (app) => { const medias = await Promise.all(
const integration = app.integration!; await mediaApps.map(async (app) => {
const endpoint = IntegrationTypeEndpointMap.get(integration.type); const integration = app.integration!;
if (!endpoint) { const endpoint = IntegrationTypeEndpointMap.get(integration.type);
return { if (!endpoint) {
type: integration.type, return {
items: [], type: integration.type,
}; items: [],
} };
}
// Get the origin URL // Get the origin URL
let { href: origin } = new URL(app.url); let { href: origin } = new URL(app.url);
if (origin.endsWith('/')) { if (origin.endsWith('/')) {
origin = origin.slice(0, -1); origin = origin.slice(0, -1);
} }
const start = new Date(year, month - 1, 1); // First day of month const start = new Date(year, month - 1, 1); // First day of month
const end = new Date(year, month, 0); // Last day of month const end = new Date(year, month, 0); // Last day of month
const apiKey = integration.properties.find((x) => x.field === 'apiKey')?.value; const apiKey = integration.properties.find((x) => x.field === 'apiKey')?.value;
if (!apiKey) return { type: integration.type, items: [] }; if (!apiKey) return { type: integration.type, items: [] };
return axios return axios
.get( .get(
`${origin}${endpoint}?apiKey=${apiKey}&end=${end.toISOString()}&start=${start.toISOString()}` `${origin}${endpoint}?apiKey=${apiKey}&end=${end.toISOString()}&start=${start.toISOString()}`
) )
.then((x) => ({ type: integration.type, items: x.data as any[] })); .then((x) => ({ type: integration.type, items: x.data as any[] }));
}) })
); );
// FIXME: I need an integration for each of them return res.status(200).json({
return res.status(200).json({ tvShows: medias.filter((m) => m.type === 'sonarr').flatMap((m) => m.items),
tvShows: medias.filter((m) => m.type === 'sonarr').flatMap((m) => m.items), movies: medias.filter((m) => m.type === 'radarr').flatMap((m) => m.items),
movies: medias.filter((m) => m.type === 'radarr').flatMap((m) => m.items), books: medias.filter((m) => m.type === 'readarr').flatMap((m) => m.items),
books: medias.filter((m) => m.type === 'readarr').flatMap((m) => m.items), musics: medias.filter((m) => m.type === 'lidarr').flatMap((m) => m.items),
musics: medias.filter((m) => m.type === 'lidarr').flatMap((m) => m.items), totalCount: medias.reduce((p, c) => p + c.items.length, 0),
totalCount: medias.reduce((p, c) => p + c.items.length, 0), });
}); } catch (error) {
Consola.error(`Error while requesting media from your app. Check your configuration. ${error}`);
return res.status(500).json({
tvShows: [],
movies: [],
books: [],
musics: [],
totalCount: 0,
});
}
} }
const IntegrationTypeEndpointMap = new Map<AppIntegrationType['type'], string>([ const IntegrationTypeEndpointMap = new Map<AppIntegrationType['type'], string>([