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
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" });
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue