feat: improve error handling for authentication flow
This commit is contained in:
@@ -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}`,
|
||||
|
||||
Reference in New Issue
Block a user