From 76ed45558d90f75e639c7b6ca6c23ad7b60d1f0a Mon Sep 17 00:00:00 2001 From: Rushil Perera Date: Tue, 10 Sep 2024 23:04:06 -0400 Subject: [PATCH] fix: internal "new episode" route throwing error code 1042 in prod --- .../episodes/getByAniListId/index.ts | 20 ++++--- .../episodes/getEpisodeUrl/index.ts | 59 ++++++++++++------- src/controllers/internal/new-episode/index.ts | 46 +++++++++------ 3 files changed, 77 insertions(+), 48 deletions(-) diff --git a/src/controllers/episodes/getByAniListId/index.ts b/src/controllers/episodes/getByAniListId/index.ts index 0bfc8d7..1e0d83a 100644 --- a/src/controllers/episodes/getByAniListId/index.ts +++ b/src/controllers/episodes/getByAniListId/index.ts @@ -43,14 +43,9 @@ const route = createRoute({ const app = new OpenAPIHono(); -app.openapi(route, async (c) => { - const aniListId = Number(c.req.param("aniListId")); - - const { result: episodes, errorOccurred } = await fetchFromMultipleSources([ - () => { - const isAnifyEnabled = readEnvVariable(c.env, "ENABLE_ANIFY"); - return getEpisodesFromAnify(isAnifyEnabled, aniListId); - }, +export function fetchEpisodes(aniListId: number, isAnifyEnabled: boolean) { + return fetchFromMultipleSources([ + () => getEpisodesFromAnify(isAnifyEnabled, aniListId), // () => // import("./consumet").then(({ getEpisodesFromConsumet }) => // getEpisodesFromConsumet(aniListId), @@ -60,6 +55,15 @@ app.openapi(route, async (c) => { getEpisodesFromAniwatch(aniListId), ), ]); +} + +app.openapi(route, async (c) => { + const aniListId = Number(c.req.param("aniListId")); + + const { result: episodes, errorOccurred } = await fetchEpisodes( + aniListId, + readEnvVariable(c.env, "ENABLE_ANIFY"), + ); if (errorOccurred) { return c.json(ErrorResponse, { status: 500 }); diff --git a/src/controllers/episodes/getEpisodeUrl/index.ts b/src/controllers/episodes/getEpisodeUrl/index.ts index 51710f2..d330af3 100644 --- a/src/controllers/episodes/getEpisodeUrl/index.ts +++ b/src/controllers/episodes/getEpisodeUrl/index.ts @@ -68,28 +68,26 @@ const route = createRoute({ const app = new OpenAPIHono(); -app.openapi(route, async (c) => { - const aniListId = Number(c.req.param("aniListId")); - const { provider, id } = await c.req.json(); - - const isAnifyEnabled = readEnvVariable(c.env, "ENABLE_ANIFY"); +export async function fetchEpisodeUrl( + provider: string, + id: string, + aniListId: number, + isAnifyEnabled: boolean, +) { if (provider === "consumet" || !isAnifyEnabled) { try { const result = await import("./consumet").then( ({ getSourcesFromConsumet }) => getSourcesFromConsumet(id), ); if (!result) { - return c.json({ success: false }, { status: 404 }); + return null; } - return c.json({ - success: true, - result, - }); + return result; } catch (e) { console.error("Failed to fetch download URL from Consumet", e); - return c.json(ErrorResponse, { status: 500 }); + throw e; } } @@ -99,17 +97,14 @@ app.openapi(route, async (c) => { ({ getSourcesFromAniwatch }) => getSourcesFromAniwatch(id), ); if (!result) { - return c.json({ success: false }, { status: 404 }); + return null; } - return c.json({ - success: true, - result, - }); + return result; } catch (e) { console.error("Failed to fetch download URL from Aniwatch", e); - return c.json(ErrorResponse, { status: 500 }); + throw e; } } @@ -118,16 +113,36 @@ app.openapi(route, async (c) => { getSourcesFromAnify(provider, id, aniListId), ); if (!result) { - return c.json({ success: false }, { status: 404 }); + return null; } - return c.json({ - success: true, - result, - }); + return result; } catch (e) { console.error("Failed to fetch download URL from Anify", e); + throw e; + } +} + +app.openapi(route, async (c) => { + const aniListId = Number(c.req.param("aniListId")); + const { provider, id } = await c.req.json(); + + try { + const isAnifyEnabled = readEnvVariable(c.env, "ENABLE_ANIFY"); + const result = await fetchEpisodeUrl( + provider, + id, + aniListId, + isAnifyEnabled, + ); + + if (result) { + return c.json({ success: true, result }); + } else { + return c.json(ErrorResponse, { status: 404 }); + } + } catch (error) { return c.json(ErrorResponse, { status: 500 }); } }); diff --git a/src/controllers/internal/new-episode/index.ts b/src/controllers/internal/new-episode/index.ts index ad629d4..58770d4 100644 --- a/src/controllers/internal/new-episode/index.ts +++ b/src/controllers/internal/new-episode/index.ts @@ -5,6 +5,8 @@ import { env } from "hono/adapter"; import mapKeys from "lodash.mapkeys"; import { z } from "zod"; +import { fetchEpisodes } from "~/controllers/episodes/getByAniListId"; +import { fetchEpisodeUrl } from "~/controllers/episodes/getEpisodeUrl"; import { Case, changeStringCase } from "~/libs/changeStringCase"; import type { AdminSdkCredentials } from "~/libs/fcm/getGoogleAuthToken"; import { sendFcmMessage } from "~/libs/fcm/sendFcmMessage"; @@ -48,10 +50,15 @@ app.post( } const domain = getCurrentDomain(c.req); - const { success, result: fetchEpisodesResult } = await fetch( - `${domain}/episodes/${aniListId}`, - ).then((res) => res.json()); - if (!success) { + const isAnifyEnabled = readEnvVariable( + env(c, "workerd"), + "ENABLE_ANIFY", + ); + const { + errorOccurred: fetchEpisodesErrorOccurred, + result: fetchEpisodesResult, + } = await fetchEpisodes(aniListId, isAnifyEnabled); + if (fetchEpisodesErrorOccurred) { console.error(`Failed to fetch episodes for title ${aniListId}`); await scheduleRetry( readEnvVariable(env(c, "workerd"), "QSTASH_TOKEN"), @@ -72,20 +79,23 @@ app.post( return c.json(ErrorResponse, { status: 404 }); } - const { success: fetchUrlSuccess, result: fetchUrlResult } = await fetch( - `${domain}/episodes/${aniListId}/url`, - { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - id: episode.id, - provider: providerId, - }), - }, - ).then((res) => res.json()); - if (!fetchUrlSuccess) { + let fetchUrlResult; + try { + fetchUrlResult = await fetchEpisodeUrl( + providerId, + episode.id, + aniListId, + isAnifyEnabled, + ); + if (!fetchUrlResult) { + console.error(`Failed to fetch episode URL for episode ${episode.id}`); + await scheduleRetry( + readEnvVariable(env(c, "workerd"), "QSTASH_TOKEN"), + c.req, + ); + return c.json(ErrorResponse, { status: 404 }); + } + } catch (error) { console.error(`Failed to fetch episode URL for episode ${episode.id}`); await scheduleRetry( readEnvVariable(env(c, "workerd"), "QSTASH_TOKEN"),