3
votes

I have a sample application created in NestJs which does not have a single controller. I have created global exception filter and injected it via app module like:

providers: [
        {
            provide: APP_FILTER,
            useClass: CustomExceptionFilter,
        },
    ]

When any service method called and exception occurs, call is not coming in my custom exception filter. Note that I have not a single controller in my application. There are modules and services only.

Also I have tried registering my custom filter in main.ts which is not working:

app.useGlobalFilters(new CustomExceptionFilter());

I want to handle exceptions of all type using global exception filter only. Any help is appreciated.

1

1 Answers

3
votes

The global aproach is ok you only need to put an empty catch decorator at the beggining of the class and it will catch all the errors

@Catch()
export class CustomExceptionsFilter implements ExceptionFilter {
  catch(exception: unknown, host: ArgumentsHost) {
  ...
  }
}