chess.js
chess.js copied to clipboard
Move Error Type
I wanted to toss this suggestion in the mix before 1.0.0 is released. 🙂
It'd be great if Chess.js could throw a specific error type if an illegal move is made, rather than a generic error. Then, detecting an illegal move could look something like this:
function reduceLegalMove(fen: string, { from, to }: MoveEvent): string {
const chess = new Chess(fen);
try {
chess.move({ from, to });
}
catch (error) {
if (error instanceof IllegalMoveError) {
return fen;
}
throw error;
}
return chess.fen();
}