From 36312d56f61cca301d99567b8eb3aa765358a308 Mon Sep 17 00:00:00 2001 From: Jonas Date: Sun, 19 Jun 2022 11:39:10 -0300 Subject: [PATCH] feat(http): created http exception filter --- src/modules/http/http-exception.filter.ts | 24 +++++++++++++++++++++++ src/modules/http/index.ts | 1 + 2 files changed, 25 insertions(+) create mode 100644 src/modules/http/http-exception.filter.ts create mode 100644 src/modules/http/index.ts 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';