fix : handle lichess eval api error

This commit is contained in:
GuillaumeSD
2025-04-20 04:21:01 +02:00
parent 3e9523c49f
commit 6c0da2a1f0
2 changed files with 21 additions and 18 deletions

View File

@@ -26,7 +26,7 @@ export const useEngine = (
return newEngine; return newEngine;
}); });
}); });
}, [engineName]); }, [engineName, workersNb]);
return engine; return engine;
}; };

View File

@@ -13,15 +13,7 @@ export const getLichessEval = async (
multiPv = 1 multiPv = 1
): Promise<PositionEval> => { ): Promise<PositionEval> => {
try { try {
const controller = new AbortController(); const data = await fetchLichessEval(fen, multiPv);
const timeoutId = setTimeout(() => controller.abort("timeout"), 200);
const res = await fetch(
`https://lichess.org/api/cloud-eval?fen=${fen}&multiPv=${multiPv}`,
{ method: "GET", signal: controller.signal }
);
clearTimeout(timeoutId);
const data: LichessResponse<LichessEvalBody> = await res.json();
if ("error" in data) { if ("error" in data) {
if (data.error === LichessError.NotFound) { if (data.error === LichessError.NotFound) {
@@ -53,9 +45,7 @@ export const getLichessEval = async (
lines: linesToKeep, lines: linesToKeep,
}; };
} catch (error) { } catch (error) {
if (!isAbortError(error)) { logErrorToSentry(error, { fen, multiPv });
logErrorToSentry(error, { fen, multiPv });
}
return { return {
bestMove: "", bestMove: "",
@@ -64,11 +54,6 @@ export const getLichessEval = async (
} }
}; };
const isAbortError = (error: unknown): boolean =>
error === "timeout" ||
((error instanceof Error || error instanceof DOMException) &&
(error.name === "AbortError" || error.message === "timeout"));
export const getLichessUserRecentGames = async ( export const getLichessUserRecentGames = async (
username: string username: string
): Promise<LichessGame[]> => { ): Promise<LichessGame[]> => {
@@ -87,3 +72,21 @@ export const getLichessUserRecentGames = async (
return games; return games;
}; };
const fetchLichessEval = async (
fen: string,
multiPv: number
): Promise<LichessResponse<LichessEvalBody>> => {
try {
const res = await fetch(
`https://lichess.org/api/cloud-eval?fen=${fen}&multiPv=${multiPv}`,
{ method: "GET", signal: AbortSignal.timeout(200) }
);
return res.json();
} catch (error) {
console.error(error);
return { error: LichessError.NotFound };
}
};