diff --git a/src/modules/http/http-exception.filter.ts b/src/modules/http/http-exception.filter.ts new file mode 100644 index 0000000..4c6be1c --- /dev/null +++ b/src/modules/http/http-exception.filter.ts @@ -0,0 +1,24 @@ +import { + ExceptionFilter, + Catch, + ArgumentsHost, + HttpException, +} from '@nestjs/common'; +import { Request, Response } from 'express'; + +@Catch(HttpException) +export class HttpExceptionFilter implements ExceptionFilter { + catch(exception: HttpException, host: ArgumentsHost) { + const ctx = host.switchToHttp(); + const response = ctx.getResponse(); + const request = ctx.getRequest(); + const status = exception.getStatus(); + + response.status(status).json({ + error: exception.message, + statusCode: status, + timestamp: new Date().toISOString(), + path: request.url, + }); + } +} diff --git a/src/modules/http/index.ts b/src/modules/http/index.ts new file mode 100644 index 0000000..18204b0 --- /dev/null +++ b/src/modules/http/index.ts @@ -0,0 +1 @@ +export * from './http-exception.filter';