fix : handle lichess eval api error
This commit is contained in:
@@ -26,7 +26,7 @@ export const useEngine = (
|
|||||||
return newEngine;
|
return newEngine;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}, [engineName]);
|
}, [engineName, workersNb]);
|
||||||
|
|
||||||
return engine;
|
return engine;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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 };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user