Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE REQUEST] filter for capturing HttpException #2147

Open
jpage-godaddy opened this issue Dec 5, 2024 · 0 comments
Open

[FEATURE REQUEST] filter for capturing HttpException #2147

jpage-godaddy opened this issue Dec 5, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@jpage-godaddy
Copy link

Is your feature request related to a problem? Please describe.
I noticed that the customErrorObject callback for pino-http was not being triggered when an error deriving from HttpException was thrown. This is because the built-in NestJS BaseExceptionFilter was kicking in, sending a response based on the status code/content of the exception. Using LoggerErrorInterceptor globally does not work because BaseExceptionFilter converts HttpException errors to a regular "successful" response. I would like my logging code to receive HttpException objects so I can log the error response bodies.

Describe the solution you'd like
It'd be nice if this library explained the case of HttpException and HttpException-derived errors not being caught by LoggerErrorInterceptor. Luckily, the code to work around the problem is pretty simple, and maybe nestjs-pino could provide an exception filter out of the box as an alternative to LoggerErrorInterceptor:

import { Catch, ArgumentsHost } from '@nestjs/common';
import { BaseExceptionFilter } from '@nestjs/core';
import { Response } from 'express';

@Catch(Error)
export class ExceptionCapturingFilter extends BaseExceptionFilter {
  catch(exception: Error, host: ArgumentsHost) {
    // If `pino-http` sees this property, it will cause the error logging to
    // kick in.
    host.switchToHttp().getResponse<Response>().err = exception;
    super.catch(exception, host);
  }
}

Here's an example of how to inject it globally:

import { Module } from '@nestjs/common';
import { APP_FILTER } from '@nestjs/core';
import logger from './logger.js';
import { ExceptionCapturingFilter } from './exception-capturing-filter.js';

@Module({
  imports: [logger],
  providers: [
    {
      provide: APP_FILTER,
      useClass: ExceptionCapturingFilter,
    },
  ],
})
export class LoggingModule {}

Describe alternatives you've considered
I tried using LoggerErrorInterceptor globally and on each individual controller; nothing was allowing the base exception filter to be bypassed.

Additional context
None so far.

@jpage-godaddy jpage-godaddy added the enhancement New feature or request label Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants