fix: improve error semantics, clarify answer key type

This commit is contained in:
lila 2026-04-28 16:07:19 +02:00
parent 6eaf282651
commit 648c5d2979
6 changed files with 62 additions and 34 deletions

View file

@ -9,7 +9,11 @@ import type {
AnswerResult,
} from "@lila/shared";
import type { GameSessionStore } from "../gameSessionStore/index.js";
import { NotFoundError } from "../errors/AppError.js";
import {
NotFoundError,
ConflictError,
UnprocessableEntityError,
} from "../errors/AppError.js";
import { shuffleArray } from "../lib/utils.js";
export const createGameSession = async (
@ -26,10 +30,10 @@ export const createGameSession = async (
);
if (terms.length === 0) {
throw new NotFoundError("No terms found for the given filters");
throw new UnprocessableEntityError("No terms found for the given filters");
}
const answerKey = new Map<string, number>();
const answerKey = new Map<string, { correctOptionId: number }>();
const questions: GameQuestion[] = await Promise.all(
terms.map(async (term) => {
@ -62,7 +66,7 @@ export const createGameSession = async (
}));
const questionId = randomUUID();
answerKey.set(questionId, correctOptionId);
answerKey.set(questionId, { correctOptionId });
return {
questionId,
@ -90,10 +94,12 @@ export const evaluateAnswer = async (
throw new NotFoundError(`Game session not found: ${submission.sessionId}`);
}
const correctOptionId = session.answers.get(submission.questionId);
const answer = session.answers.get(submission.questionId);
if (correctOptionId === undefined) {
throw new NotFoundError(`Question not found: ${submission.questionId}`);
if (answer === undefined) {
throw new ConflictError(
`Question already answered: ${submission.questionId}`,
);
}
const updatedAnswers = new Map(session.answers);
@ -110,8 +116,8 @@ export const evaluateAnswer = async (
return {
questionId: submission.questionId,
isCorrect: submission.selectedOptionId === correctOptionId,
correctOptionId,
isCorrect: submission.selectedOptionId === answer.correctOptionId,
correctOptionId: answer.correctOptionId,
selectedOptionId: submission.selectedOptionId,
};
};