feat: add more error handling
This commit is contained in:
@@ -11,52 +11,38 @@ export async function fetchTitleFromAmvstrm(
|
||||
console.error("Failed to get missing information from Anify", err);
|
||||
return null;
|
||||
}),
|
||||
]).then(
|
||||
async ([
|
||||
{
|
||||
id,
|
||||
idMal,
|
||||
title: { english: englishTitle, userPreferred: userPreferredTitle },
|
||||
description,
|
||||
episodes,
|
||||
genres,
|
||||
status,
|
||||
bannerImage,
|
||||
coverImage: {
|
||||
extraLarge: extraLargeCoverImage,
|
||||
large: largeCoverImage,
|
||||
medium: mediumCoverImage,
|
||||
},
|
||||
countryOfOrigin,
|
||||
nextair: nextAiringEpisode,
|
||||
score: { averageScore },
|
||||
]).then(async ([amvstrmInfo, anifyInfo]) => {
|
||||
if (amvstrmInfo.code >= 400) {
|
||||
console.error(
|
||||
`Error trying to load title from amvstrm; aniListId: ${aniListId}, code: ${amvstrmInfo.code}, message: ${amvstrmInfo.message}`,
|
||||
);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return {
|
||||
id: amvstrmInfo.id,
|
||||
idMal: amvstrmInfo.idMal,
|
||||
title: {
|
||||
userPreferred: amvstrmInfo.title.userPreferred,
|
||||
english: amvstrmInfo.title.english,
|
||||
},
|
||||
anifyInfo,
|
||||
]) => {
|
||||
return {
|
||||
id,
|
||||
idMal,
|
||||
title: {
|
||||
userPreferred: userPreferredTitle,
|
||||
english: englishTitle,
|
||||
},
|
||||
description,
|
||||
episodes,
|
||||
genres,
|
||||
status,
|
||||
averageScore,
|
||||
bannerImage: bannerImage ?? anifyInfo?.bannerImage,
|
||||
coverImage: {
|
||||
extraLarge: extraLargeCoverImage,
|
||||
large: largeCoverImage,
|
||||
medium: mediumCoverImage,
|
||||
},
|
||||
countryOfOrigin: countryOfOrigin ?? anifyInfo?.countryOfOrigin,
|
||||
nextAiringEpisode,
|
||||
mediaListEntry: null,
|
||||
};
|
||||
},
|
||||
);
|
||||
description: amvstrmInfo.description,
|
||||
episodes: amvstrmInfo.episodes,
|
||||
genres: amvstrmInfo.genres,
|
||||
status: amvstrmInfo.status,
|
||||
averageScore: amvstrmInfo.score.averageScore,
|
||||
bannerImage: amvstrmInfo.bannerImage ?? anifyInfo?.bannerImage,
|
||||
coverImage: {
|
||||
extraLarge: amvstrmInfo.coverImage.extraLarge,
|
||||
large: amvstrmInfo.coverImage.large,
|
||||
medium: amvstrmInfo.coverImage.medium,
|
||||
},
|
||||
countryOfOrigin:
|
||||
amvstrmInfo.countryOfOrigin ?? anifyInfo?.countryOfOrigin,
|
||||
nextAiringEpisode: amvstrmInfo.nextair,
|
||||
mediaListEntry: null,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
type AnifyInformation = {
|
||||
|
||||
@@ -28,5 +28,12 @@ export async function fetchTitleFromAnilist(
|
||||
|
||||
return client
|
||||
.request(GetTitleQuery, { id }, headers)
|
||||
.then((data) => data?.Media ?? undefined);
|
||||
.then((data) => data?.Media ?? undefined)
|
||||
.catch((error) => {
|
||||
if (error.message.includes("Not Found")) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
||||
import { fetchFromMultipleSources } from "~/libs/fetchFromMultipleSources";
|
||||
import {
|
||||
AniListIdQuerySchema,
|
||||
ErrorResponse,
|
||||
ErrorResponseSchema,
|
||||
SuccessResponseSchema,
|
||||
} from "~/types/schema";
|
||||
@@ -47,12 +48,17 @@ app.openapi(route, async (c) => {
|
||||
const aniListId = Number(c.req.query("id"));
|
||||
const aniListToken = c.req.header("X-AniList-Token");
|
||||
|
||||
const title = await fetchFromMultipleSources([
|
||||
const { result: title, errors } = await fetchFromMultipleSources([
|
||||
() => fetchTitleFromAnilist(aniListId, aniListToken ?? undefined),
|
||||
() => fetchTitleFromAmvstrm(aniListId),
|
||||
]);
|
||||
|
||||
if (errors?.length > 0) {
|
||||
return c.json(ErrorResponse, { status: 500 });
|
||||
}
|
||||
|
||||
if (!title) {
|
||||
return c.json({ success: false }, 404);
|
||||
return c.json(ErrorResponse, 404);
|
||||
}
|
||||
|
||||
return c.json({ success: true, result: title }, 200);
|
||||
|
||||
Reference in New Issue
Block a user