Global Exception Filter not catching some errors

Viewed 138

I've recently created a Global exception filter, but I just noticed that, some errors are not being caught by it, like:

TypeError: Cannot read properties of undefined (reading 'dataNumber')

My filter:


@Catch()
export class GlobalExceptionsFilter implements ExceptionFilter {
  constructor(
    private readonly httpAdapterHost: HttpAdapterHost,
    private readonly logger: LoggerService,
  ) {}

  catch(exception: Error, host: ArgumentsHost): void {
    const { httpAdapter } = this.httpAdapterHost;

    const ctx = host.switchToHttp();

    const httpStatus =
      exception instanceof HttpException
        ? exception.getStatus()
        : HttpStatus.INTERNAL_SERVER_ERROR;

    const responseBody = {
      statusCode: httpStatus,
      path: httpAdapter.getRequestUrl(ctx.getRequest()),
      cause: `${exception.name}: ${exception.message}`,
      timestamp: new Date().toISOString(),
    };

    this.logger.error(responseBody?.cause);

    httpAdapter.reply(ctx.getResponse(), responseBody, httpStatus);
  }

And I'm using it as a provider on my App.module:

    {
      provide: APP_FILTER,
      useClass: GlobalExceptionsFilter,
    },

Currently, I also have a SCPModule which is being imported on the same App.module, that error is happening on the SCPService inside the SCPModule... Does every module needs to use my filter as a provider?

0 Answers
Related