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:
lila 2026-04-12 08:48:43 +02:00
parent dd6c2b0118
commit 48457936e8
7 changed files with 114 additions and 24 deletions

View 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" });
};