feat: improve error handling for authentication flow

This commit is contained in:
2024-09-26 03:24:15 -04:00
parent 7a839cda5a
commit bee8acaca8
9 changed files with 143 additions and 47 deletions

View File

@@ -1,8 +1,10 @@
import { findBestMatchingTitle } from "~/libs/findBestMatchingTitle";
import { sleep } from "~/libs/sleep";
import { Episode, type EpisodesResponse } from "~/types/episode";
export async function getEpisodesFromAniwatch(
aniListId: number,
shouldRetry: boolean = false,
): Promise<EpisodesResponse | null> {
try {
const animeTitle = await import("~/libs/anilist/getTitle")
@@ -48,6 +50,16 @@ export async function getEpisodesFromAniwatch(
return { providerId: "aniwatch", episodes };
} catch (error) {
if (shouldRetry && "response" in error && error.response.status === 429) {
console.log(
"429, retrying in",
error.response.headers.get("Retry-After"),
);
return sleep(
Number(error.response.headers.get("Retry-After")!) * 1000,
).then(() => getEpisodesFromAniwatch(aniListId));
}
console.error(
new Error(
`Error trying to load episodes from aniwatch; aniListId: ${aniListId}`,