U
U
user_of_toster2021-06-24 15:52:11
Node.js
user_of_toster, 2021-06-24 15:52:11

Why inject httpServer into Nest's global filter?

I understood this: in order for our filter to extend the behavior of the base filter, it is enough to inherit the functionality from BaseExceptionFilter, and then bind the filter like this:

class FilterThatExtendsBaseExceptionFilter extends BaseExceptionFilter {...}

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalFilters(new FilterThatExtendsBaseExceptionFilter()); // здесь httpAdapter'a нет
  await app.listen(3000);
}

The question is - why, in addition to inheritance, also inject httpAdapterif we have already inherited functionality from BaseExceptionFilter?
const { httpAdapter } = app.get(HttpAdapterHost);
app.useGlobalFilters(new AllExceptionsFilter(httpAdapter)); // зачем здесь httpAdapter?

Global filters can extend the base filter. This can be done in either of two ways.
The first method is to inject the HttpServer reference when instantiating the custom global filter:

https://docs.nestjs.com/exception-filters#inheritance

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question