title: Global error filter catches no exceptions from services, only controllers
body: <p>I have an api NestJS where I implemented an error filter to catch all kinds of exceptions. In main.ts I set the api to use the filter globally.</p>
  
  <p>Apparently, it only catches the errors in the controllers, because when I throw an exception in the context of service, the exception is thrown in the console and the api falls.</p>
  
  <p>main.ts:</p>
  
  <pre><code>import { NestFactory } from '@nestjs/core';
  import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
  import {
    FastifyAdapter,
    NestFastifyApplication,
  } from '@nestjs/platform-fastify';
  import { AppModule } from './app.module';
  import { HttpExceptionFilter } from './exception-filters/http-exception.filter';
  import { AllExceptionsFilter } from './exception-filters/exception.filter';
  import { ValidationPipe } from '@nestjs/common';
  
  async function bootstrap() {
    const app = await NestFactory.create&lt;NestFastifyApplication&gt;(
      AppModule,
      new FastifyAdapter());
  
    const options = new DocumentBuilder()
      .setTitle('API Agendamento.Vip')
      // .setDescription('')
      .setVersion('1.0')
      .build();
  
    const document = SwaggerModule.createDocument(app, options);
    SwaggerModule.setup('api', app, document);
    // app.useGlobalFilters(new HttpExceptionFilter());
    app.useGlobalPipes(new ValidationPipe());
    app.useGlobalFilters(new AllExceptionsFilter());
    await app.listen(process.env.PORT || 3001);
  }
  bootstrap();
  </code></pre>
  
  <p>in a service method these exceptions are thrown</p>
  
  <pre><code>  if (err) { throw new InternalServerErrorException('error', err); }
  
    if (!user) { throw new NotFoundException('device info missing'); }
  
    if (!user.active) { throw new HttpException('active error', 400); }
  
  </code></pre>
  
  <p>my exception filter: </p>
  
  <pre><code>import { ExceptionFilter, Catch, ArgumentsHost, HttpException, HttpStatus, BadRequestException } from '@nestjs/common';
  import { FastifyRequest, FastifyReply } from 'fastify';
  
  @Catch()
  export class AllExceptionsFilter implements ExceptionFilter {
    catch(exception: unknown, host: ArgumentsHost) {
      const ctx = host.switchToHttp();
      const response: FastifyReply&lt;any&gt; = ctx.getResponse();
      const request: FastifyRequest = ctx.getRequest();
  
      const status =
        exception instanceof HttpException
          ? exception.getStatus()
          : HttpStatus.INTERNAL_SERVER_ERROR;
  
      const objResponse = Object.assign(exception, {
        timestamp: new Date().toISOString(),
        path: request.req.url
      });
  
      response.status(status).send({
        objResponse
      });
    }
  }
  
  </code></pre>
  
  <p>Exceptions are thrown on the Node console, causing the api to stop.</p>
  
  <p>They are only captured by the filter if they are in the context of the Controller.</p>
  
  <p>What can I do to get exceptions caught in the service as well?</p>