|
| 1 | +/** |
| 2 | + * Custom Error Classes |
| 3 | + * |
| 4 | + * These error classes provide a consistent way to handle errors throughout |
| 5 | + * the application. They extend the base AppError class and are automatically |
| 6 | + * handled by the error handler middleware. |
| 7 | + * |
| 8 | + * Usage: |
| 9 | + * throw new NotFoundError('Story'); |
| 10 | + * throw new ValidationError('Invalid input', { field: ['Error message'] }); |
| 11 | + * throw new AuthenticationError('Invalid credentials'); |
| 12 | + * |
| 13 | + * Error Handler: |
| 14 | + * All errors are caught by the error handler middleware in |
| 15 | + * middlewares/errors/error-handler.ts which formats the response. |
| 16 | + */ |
| 17 | + |
| 18 | +/** |
| 19 | + * Base error class for all application errors |
| 20 | + * @param statusCode - HTTP status code |
| 21 | + * @param message - Error message |
| 22 | + * @param isOperational - Whether this is an operational error (vs programming error) |
| 23 | + * @param code - Error code for client-side handling |
| 24 | + */ |
| 25 | +export class AppError extends Error { |
| 26 | + constructor( |
| 27 | + public statusCode: number, |
| 28 | + public message: string, |
| 29 | + public isOperational = true, |
| 30 | + public code?: string |
| 31 | + ) { |
| 32 | + super(message); |
| 33 | + Object.setPrototypeOf(this, AppError.prototype); |
| 34 | + Error.captureStackTrace(this, this.constructor); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Validation error (400) - Used when request validation fails |
| 40 | + * @param message - Error message |
| 41 | + * @param errors - Field-specific validation errors |
| 42 | + */ |
| 43 | +export class ValidationError extends AppError { |
| 44 | + constructor(message: string, public errors?: Record<string, string[]>) { |
| 45 | + super(400, message, true, 'VALIDATION_ERROR'); |
| 46 | + this.name = 'ValidationError'; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Authentication error (401) - Used when user is not authenticated |
| 52 | + * @param message - Error message (default: 'Authentication required') |
| 53 | + */ |
| 54 | +export class AuthenticationError extends AppError { |
| 55 | + constructor(message: string = 'Authentication required') { |
| 56 | + super(401, message, true, 'AUTHENTICATION_ERROR'); |
| 57 | + this.name = 'AuthenticationError'; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Authorization error (403) - Used when user lacks required permissions |
| 63 | + * @param message - Error message (default: 'Insufficient permissions') |
| 64 | + */ |
| 65 | +export class AuthorizationError extends AppError { |
| 66 | + constructor(message: string = 'Insufficient permissions') { |
| 67 | + super(403, message, true, 'AUTHORIZATION_ERROR'); |
| 68 | + this.name = 'AuthorizationError'; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Not found error (404) - Used when a resource doesn't exist |
| 74 | + * @param resource - Resource name (e.g., 'Story', 'User') |
| 75 | + */ |
| 76 | +export class NotFoundError extends AppError { |
| 77 | + constructor(resource: string = 'Resource') { |
| 78 | + super(404, `${resource} not found`, true, 'NOT_FOUND'); |
| 79 | + this.name = 'NotFoundError'; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Conflict error (409) - Used when there's a resource conflict |
| 85 | + * @param message - Error message describing the conflict |
| 86 | + */ |
| 87 | +export class ConflictError extends AppError { |
| 88 | + constructor(message: string) { |
| 89 | + super(409, message, true, 'CONFLICT'); |
| 90 | + this.name = 'ConflictError'; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Internal server error (500) - Used for unexpected server errors |
| 96 | + * @param message - Error message (default: 'Internal server error') |
| 97 | + */ |
| 98 | +export class InternalServerError extends AppError { |
| 99 | + constructor(message: string = 'Internal server error') { |
| 100 | + super(500, message, false, 'INTERNAL_ERROR'); |
| 101 | + this.name = 'InternalServerError'; |
| 102 | + } |
| 103 | +} |
0 commit comments