http-exception.filter.ts106 lines, 2.8 KB
| 1 | import { |
| 2 | ExceptionFilter, |
| 3 | Catch, |
| 4 | ArgumentsHost, |
| 5 | HttpException, |
| 6 | HttpStatus, |
| 7 | } from '@nestjs/common'; |
| 8 | import { Request, Response } from 'express'; |
| 9 | |
| 10 | const ERROR_CODES: Record<number, string> = { |
| 11 | 400: 'BAD_REQUEST', |
| 12 | 401: 'UNAUTHORIZED', |
| 13 | 403: 'FORBIDDEN', |
| 14 | 404: 'NOT_FOUND', |
| 15 | 409: 'CONFLICT', |
| 16 | 422: 'VALIDATION_ERROR', |
| 17 | 429: 'TOO_MANY_REQUESTS', |
| 18 | 500: 'INTERNAL_SERVER_ERROR', |
| 19 | }; |
| 20 | |
| 21 | export interface ApiErrorResponse { |
| 22 | success: false; |
| 23 | error: { |
| 24 | code: string; |
| 25 | message: string; |
| 26 | details?: unknown[]; |
| 27 | timestamp: string; |
| 28 | path: string; |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | @Catch(HttpException) |
| 33 | export class HttpExceptionFilter implements ExceptionFilter { |
| 34 | catch(exception: HttpException, host: ArgumentsHost) { |
| 35 | const ctx = host.switchToHttp(); |
| 36 | const response = ctx.getResponse<Response>(); |
| 37 | const request = ctx.getRequest<Request>(); |
| 38 | const status = exception.getStatus(); |
| 39 | |
| 40 | const exceptionResponse = exception.getResponse(); |
| 41 | |
| 42 | let message: string; |
| 43 | let details: unknown[] | undefined; |
| 44 | |
| 45 | if (typeof exceptionResponse === 'string') { |
| 46 | message = exceptionResponse; |
| 47 | } else if (typeof exceptionResponse === 'object') { |
| 48 | const responseObj = exceptionResponse as Record<string, unknown>; |
| 49 | |
| 50 | if (Array.isArray(responseObj.message)) { |
| 51 | message = 'Validation failed'; |
| 52 | details = responseObj.message; |
| 53 | } else { |
| 54 | message = (responseObj.message as string) || exception.message; |
| 55 | if (responseObj.error && typeof responseObj.error !== 'string') { |
| 56 | details = [responseObj.error]; |
| 57 | } |
| 58 | } |
| 59 | } else { |
| 60 | message = exception.message; |
| 61 | } |
| 62 | |
| 63 | const errorResponse: ApiErrorResponse = { |
| 64 | success: false, |
| 65 | error: { |
| 66 | code: ERROR_CODES[status] || `HTTP_${status}`, |
| 67 | message, |
| 68 | ...(details && { details }), |
| 69 | timestamp: new Date().toISOString(), |
| 70 | path: request.url, |
| 71 | }, |
| 72 | }; |
| 73 | |
| 74 | response.status(status).json(errorResponse); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | @Catch() |
| 79 | export class AllExceptionsFilter implements ExceptionFilter { |
| 80 | catch(exception: unknown, host: ArgumentsHost) { |
| 81 | const ctx = host.switchToHttp(); |
| 82 | const response = ctx.getResponse<Response>(); |
| 83 | const request = ctx.getRequest<Request>(); |
| 84 | |
| 85 | const status = HttpStatus.INTERNAL_SERVER_ERROR; |
| 86 | const message = |
| 87 | exception instanceof Error ? exception.message : 'Internal server error'; |
| 88 | |
| 89 | console.error('Unhandled exception:', exception); |
| 90 | |
| 91 | const errorResponse: ApiErrorResponse = { |
| 92 | success: false, |
| 93 | error: { |
| 94 | code: 'INTERNAL_SERVER_ERROR', |
| 95 | message: |
| 96 | process.env.NODE_ENV === 'production' |
| 97 | ? 'Internal server error' |
| 98 | : message, |
| 99 | timestamp: new Date().toISOString(), |
| 100 | path: request.url, |
| 101 | }, |
| 102 | }; |
| 103 | |
| 104 | response.status(status).json(errorResponse); |
| 105 | } |
| 106 | } |