48 lines · 1.1 KB
1 /** Application error types. */
2
3 export class AppError extends Error {
4 constructor(
5 message: string,
6 public readonly statusCode: number = 500,
7 public readonly code: string = 'INTERNAL_ERROR'
8 ) {
9 super(message);
10 this.name = 'AppError';
11 }
12 }
13
14 export class NotFoundError extends AppError {
15 constructor(message = 'Not found') {
16 super(message, 404, 'NOT_FOUND');
17 this.name = 'NotFoundError';
18 }
19 }
20
21 export class UnauthorizedError extends AppError {
22 constructor(message = 'Unauthorized') {
23 super(message, 401, 'UNAUTHORIZED');
24 this.name = 'UnauthorizedError';
25 }
26 }
27
28 export class ForbiddenError extends AppError {
29 constructor(message = 'Forbidden') {
30 super(message, 403, 'FORBIDDEN');
31 this.name = 'ForbiddenError';
32 }
33 }
34
35 export class ConflictError extends AppError {
36 constructor(message = 'Conflict') {
37 super(message, 409, 'CONFLICT');
38 this.name = 'ConflictError';
39 }
40 }
41
42 export class ValidationError extends AppError {
43 constructor(message: string) {
44 super(message, 400, 'VALIDATION_ERROR');
45 this.name = 'ValidationError';
46 }
47 }
48