Hi, I have an ExceptionFilter applied to a Controller.
The filter injects request scoped dependency (logger). I know the logger is really provided as I placed an assertion into the filter's constructor.
The controller injects request scoped dependency (dbConn), which is provided via factory.
When the factory throws, the filter catches the exception, but at that moment the logger is suddenly undefined , so an attempt to log the error crashes the app.
None of the deps are durable.
I am using 10.3.3
@Catch()
export class ExtResourceExceptionFilter
extends BaseExceptionFilter
implements ExceptionFilter
{
constructor(
@Inject(LOGGER) private readonly logger: Logger,
//httpAdapterHost: HttpAdapterHost,
) {
// Dunno if this is necessary (it's not a global filter),
// if uncommented, it throws `isHeadersSent` is not a function,
// which suggests that adapter dependency is unset in the same way as `logger` dependency
// super(httpAdapterHost.httpAdapter.getInstance());
// console.log('constructor', this.logger);
super();
assert(this.logger);
}
override catch(exception: unknown, host: ArgumentsHost) {
if (exception instanceof ParseRequestException) {
super.catch(
new BadRequestException({
status: 'fail',
message: exception.message,
}),
host,
);
} else if (exception instanceof HttpException) {
super.catch(exception, host);
} else {
// logger is undefined if factory for controller dependency throws
this.logger.log({
level: 'ERROR',
message: inspect(exception),
});
super.catch(
new InternalServerErrorException({
status: 'error',
message: 'Internal server error',
}),
host,
);
}
}
}