chess.js icon indicating copy to clipboard operation
chess.js copied to clipboard

Move Error Type

Open LandonSchropp opened this issue 11 months ago • 0 comments

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();
}

LandonSchropp avatar Jul 23 '23 04:07 LandonSchropp