feat(api): add global error handler with typed error classes
- Add AppError base class, ValidationError (400), NotFoundError (404) - Add central error middleware in app.ts - Remove inline safeParse error handling from controllers - Replace plain Error throws with NotFoundError in gameService
This commit is contained in:
parent
dd6c2b0118
commit
48457936e8
7 changed files with 114 additions and 24 deletions
|
|
@ -1,11 +1,14 @@
|
|||
import express from "express";
|
||||
import type { Express } from "express";
|
||||
import { apiRouter } from "./routes/apiRouter.js";
|
||||
import { errorHandler } from "./middleware/errorHandler.js";
|
||||
|
||||
export function createApp() {
|
||||
const app: Express = express();
|
||||
|
||||
app.use(express.json());
|
||||
app.use("/api/v1", apiRouter);
|
||||
app.use(errorHandler);
|
||||
|
||||
return app;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,32 +1,42 @@
|
|||
import type { Request, Response } from "express";
|
||||
import type { Request, Response, NextFunction } from "express";
|
||||
import { GameRequestSchema, AnswerSubmissionSchema } from "@glossa/shared";
|
||||
import { createGameSession, evaluateAnswer } from "../services/gameService.js";
|
||||
import { ValidationError } from "../errors/AppError.js";
|
||||
|
||||
export const createGame = async (req: Request, res: Response) => {
|
||||
const gameSettings = GameRequestSchema.safeParse(req.body);
|
||||
export const createGame = async (
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction,
|
||||
) => {
|
||||
try {
|
||||
const gameSettings = GameRequestSchema.safeParse(req.body);
|
||||
|
||||
// TODO: remove when global error handler is implemented
|
||||
if (!gameSettings.success) {
|
||||
res.status(400).json({ success: false });
|
||||
return;
|
||||
if (!gameSettings.success) {
|
||||
throw new ValidationError(gameSettings.error.message);
|
||||
}
|
||||
|
||||
const gameQuestions = await createGameSession(gameSettings.data);
|
||||
res.json({ success: true, data: gameQuestions });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
|
||||
const gameQuestions = await createGameSession(gameSettings.data);
|
||||
|
||||
res.json({ success: true, data: gameQuestions });
|
||||
};
|
||||
|
||||
export const submitAnswer = async (req: Request, res: Response) => {
|
||||
const submission = AnswerSubmissionSchema.safeParse(req.body);
|
||||
// TODO: remove when global error handler is implemented
|
||||
if (!submission.success) {
|
||||
res.status(400).json({ success: false });
|
||||
return;
|
||||
}
|
||||
export const submitAnswer = async (
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction,
|
||||
) => {
|
||||
try {
|
||||
const submission = AnswerSubmissionSchema.safeParse(req.body);
|
||||
|
||||
if (!submission.success) {
|
||||
throw new ValidationError(submission.error.message);
|
||||
}
|
||||
|
||||
const result = await evaluateAnswer(submission.data);
|
||||
res.json({ success: true, data: result });
|
||||
} catch (error) {
|
||||
res.status(404).json({ success: false });
|
||||
next(error);
|
||||
}
|
||||
};
|
||||
|
|
|
|||
21
apps/api/src/errors/AppError.ts
Normal file
21
apps/api/src/errors/AppError.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
export class AppError extends Error {
|
||||
public readonly statusCode: number;
|
||||
|
||||
constructor(message: string, statusCode: number) {
|
||||
super(message);
|
||||
this.name = this.constructor.name;
|
||||
this.statusCode = statusCode;
|
||||
}
|
||||
}
|
||||
|
||||
export class ValidationError extends AppError {
|
||||
constructor(message: string) {
|
||||
super(message, 400);
|
||||
}
|
||||
}
|
||||
|
||||
export class NotFoundError extends AppError {
|
||||
constructor(message: string) {
|
||||
super(message, 404);
|
||||
}
|
||||
}
|
||||
18
apps/api/src/middleware/errorHandler.ts
Normal file
18
apps/api/src/middleware/errorHandler.ts
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import type { Request, Response, NextFunction } from "express";
|
||||
import { AppError } from "../errors/AppError.js";
|
||||
|
||||
export const errorHandler = (
|
||||
err: Error,
|
||||
_req: Request,
|
||||
res: Response,
|
||||
_next: NextFunction,
|
||||
) => {
|
||||
if (err instanceof AppError) {
|
||||
res.status(err.statusCode).json({ success: false, error: err.message });
|
||||
return;
|
||||
}
|
||||
|
||||
console.error("Unexpected error:", err);
|
||||
|
||||
res.status(500).json({ success: false, error: "Internal server error" });
|
||||
};
|
||||
|
|
@ -8,8 +8,8 @@ import type {
|
|||
AnswerSubmission,
|
||||
AnswerResult,
|
||||
} from "@glossa/shared";
|
||||
|
||||
import { InMemoryGameSessionStore } from "../gameSessionStore/index.js";
|
||||
import { NotFoundError } from "../errors/AppError.js";
|
||||
|
||||
const gameSessionStore = new InMemoryGameSessionStore();
|
||||
|
||||
|
|
@ -39,7 +39,6 @@ export const createGameSession = async (
|
|||
|
||||
const optionTexts = [correctAnswer.targetText, ...distractorTexts];
|
||||
const shuffledTexts = shuffle(optionTexts);
|
||||
|
||||
const correctOptionId = shuffledTexts.indexOf(correctAnswer.targetText);
|
||||
|
||||
const options: AnswerOption[] = shuffledTexts.map((text, index) => ({
|
||||
|
|
@ -64,6 +63,7 @@ export const createGameSession = async (
|
|||
|
||||
return { sessionId, questions };
|
||||
};
|
||||
|
||||
const shuffle = <T>(array: T[]): T[] => {
|
||||
const result = [...array];
|
||||
for (let i = result.length - 1; i > 0; i--) {
|
||||
|
|
@ -81,13 +81,13 @@ export const evaluateAnswer = async (
|
|||
const session = await gameSessionStore.get(submission.sessionId);
|
||||
|
||||
if (!session) {
|
||||
throw new Error(`Game session not found: ${submission.sessionId}`);
|
||||
throw new NotFoundError(`Game session not found: ${submission.sessionId}`);
|
||||
}
|
||||
|
||||
const correctOptionId = session.answers.get(submission.questionId);
|
||||
|
||||
if (correctOptionId === undefined) {
|
||||
throw new Error(`Question not found: ${submission.questionId}`);
|
||||
throw new NotFoundError(`Question not found: ${submission.questionId}`);
|
||||
}
|
||||
|
||||
return {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue