From 396ad181d00a5a46daa5637f862aa919e29f9b4e Mon Sep 17 00:00:00 2001 From: David Mehren Date: Fri, 11 Feb 2022 19:20:54 +0100 Subject: [PATCH] fix: correctly initialize exception mapping The constructor of an exception filter must be given an instance of HttpAdapterHost, otherwise it will crash at runtime. This can be reproduced by GETing /. Reference: https://docs.nestjs.com/exception-filters#inheritance Signed-off-by: David Mehren --- src/errors/error-mapping.ts | 2 ++ src/main.ts | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/errors/error-mapping.ts b/src/errors/error-mapping.ts index 59967bf54..c1f01e5d0 100644 --- a/src/errors/error-mapping.ts +++ b/src/errors/error-mapping.ts @@ -6,6 +6,7 @@ import { ArgumentsHost, BadRequestException, + Catch, ConflictException, InternalServerErrorException, NotFoundException, @@ -67,6 +68,7 @@ const mapOfHedgeDocErrorsToHttpErrors: Map = ], ]); +@Catch() export class ErrorExceptionMapping extends BaseExceptionFilter { catch(error: Error, host: ArgumentsHost): void { super.catch(ErrorExceptionMapping.transformError(error), host); diff --git a/src/main.ts b/src/main.ts index 597ab000c..f762174be 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,7 +5,7 @@ */ import { LogLevel } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; -import { NestFactory } from '@nestjs/core'; +import { HttpAdapterHost, NestFactory } from '@nestjs/core'; import { NestExpressApplication } from '@nestjs/platform-express'; import { AppModule } from './app.module'; @@ -79,7 +79,8 @@ async function bootstrap(): Promise { app.useStaticAssets('public', { prefix: '/public/', }); - app.useGlobalFilters(new ErrorExceptionMapping()); + const { httpAdapter } = app.get(HttpAdapterHost); + app.useGlobalFilters(new ErrorExceptionMapping(httpAdapter)); await app.listen(appConfig.port); logger.log(`Listening on port ${appConfig.port}`, 'AppBootstrap'); }