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