fix: internal "new episode" route throwing error code 1042 in prod

This commit is contained in:
2024-09-10 23:04:06 -04:00
parent b3738184c9
commit 76ed45558d
3 changed files with 77 additions and 48 deletions

View File

@@ -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<EpisodesResponseSchema>());
if (!success) {
const isAnifyEnabled = readEnvVariable<boolean>(
env<Env, typeof c>(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<Env, typeof c>(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<FetchUrlResponse>());
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<Env, typeof c>(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<Env, typeof c>(c, "workerd"), "QSTASH_TOKEN"),