fix: fetchFromMultipleSources returns errorOccurred only if all sources fail
This commit is contained in:
@@ -49,7 +49,7 @@ const app = new OpenAPIHono<Env>();
|
||||
app.openapi(route, async (c) => {
|
||||
const aniListId = Number(c.req.param("aniListId"));
|
||||
|
||||
const { result: episodes, errors } = await fetchFromMultipleSources([
|
||||
const { result: episodes, errorOccurred } = await fetchFromMultipleSources([
|
||||
() => {
|
||||
const isAnifyEnabled = readEnvVariable<boolean>(c.env, "ENABLE_ANIFY");
|
||||
return getEpisodesFromAnify(isAnifyEnabled, aniListId);
|
||||
@@ -64,7 +64,7 @@ app.openapi(route, async (c) => {
|
||||
),
|
||||
]);
|
||||
|
||||
if (errors?.length > 0) {
|
||||
if (errorOccurred) {
|
||||
return c.json(ErrorResponse, { status: 500 });
|
||||
}
|
||||
|
||||
|
||||
@@ -39,14 +39,14 @@ app.openapi(route, async (c) => {
|
||||
const page = Number(c.req.query("page") ?? 1);
|
||||
const limit = Number(c.req.query("limit") ?? 10);
|
||||
|
||||
const { result: response, errors } = await fetchFromMultipleSources([
|
||||
const { result: response, errorOccurred } = await fetchFromMultipleSources([
|
||||
() => fetchSearchResultsFromAnilist(query, page, limit),
|
||||
() => fetchSearchResultsFromAmvstrm(query, page, limit),
|
||||
]);
|
||||
|
||||
if (!response) {
|
||||
return c.json({
|
||||
success: (errors ?? []).length === 0,
|
||||
success: !errorOccurred,
|
||||
results: [],
|
||||
hasNextPage: false,
|
||||
});
|
||||
|
||||
@@ -48,12 +48,12 @@ app.openapi(route, async (c) => {
|
||||
const aniListId = Number(c.req.query("id"));
|
||||
const aniListToken = c.req.header("X-AniList-Token");
|
||||
|
||||
const { result: title, errors } = await fetchFromMultipleSources([
|
||||
const { result: title, errorOccurred } = await fetchFromMultipleSources([
|
||||
() => fetchTitleFromAnilist(aniListId, aniListToken ?? undefined),
|
||||
() => fetchTitleFromAmvstrm(aniListId),
|
||||
]);
|
||||
|
||||
if (errors?.length > 0) {
|
||||
if (errorOccurred) {
|
||||
return c.json(ErrorResponse, { status: 500 });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user