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 <git@herrmehren.de>
This commit is contained in:
David Mehren 2022-02-11 19:20:54 +01:00
parent bb115dedb6
commit 2c081b8dbf
2 changed files with 5 additions and 2 deletions

View file

@ -6,6 +6,7 @@
import { import {
ArgumentsHost, ArgumentsHost,
BadRequestException, BadRequestException,
Catch,
ConflictException, ConflictException,
InternalServerErrorException, InternalServerErrorException,
NotFoundException, NotFoundException,
@ -67,6 +68,7 @@ const mapOfHedgeDocErrorsToHttpErrors: Map<string, HttpExceptionConstructor> =
], ],
]); ]);
@Catch()
export class ErrorExceptionMapping extends BaseExceptionFilter<Error> { export class ErrorExceptionMapping extends BaseExceptionFilter<Error> {
catch(error: Error, host: ArgumentsHost): void { catch(error: Error, host: ArgumentsHost): void {
super.catch(ErrorExceptionMapping.transformError(error), host); super.catch(ErrorExceptionMapping.transformError(error), host);

View file

@ -5,7 +5,7 @@
*/ */
import { LogLevel } from '@nestjs/common'; import { LogLevel } from '@nestjs/common';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { NestFactory } from '@nestjs/core'; import { HttpAdapterHost, NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express'; import { NestExpressApplication } from '@nestjs/platform-express';
import { AppModule } from './app.module'; import { AppModule } from './app.module';
@ -79,7 +79,8 @@ async function bootstrap(): Promise<void> {
app.useStaticAssets('public', { app.useStaticAssets('public', {
prefix: '/public/', prefix: '/public/',
}); });
app.useGlobalFilters(new ErrorExceptionMapping()); const { httpAdapter } = app.get(HttpAdapterHost);
app.useGlobalFilters(new ErrorExceptionMapping(httpAdapter));
await app.listen(appConfig.port); await app.listen(appConfig.port);
logger.log(`Listening on port ${appConfig.port}`, 'AppBootstrap'); logger.log(`Listening on port ${appConfig.port}`, 'AppBootstrap');
} }