How to use multiple exception filters in the main module in nestjs?

25 Views Asked by At

I built an application to extract data of some websites automatically using Puppeteer and NESTJS. I've already created a global filter to handle my exceptions and now I'm trying to create another global filter to handle only with Puppeteer exceptions to save them in log files.

Puppeter exception filter code:

import { ExceptionFilter, Catch, ArgumentsHost } from "@nestjs/common";
import { PuppeteerError } from "puppeteer";

@Catch(PuppeteerError)
export class AllPuppeteerExceptionsFilter
    implements ExceptionFilter<PuppeteerError>
{
    constructor() {}

    catch(exception: PuppeteerError, host: ArgumentsHost) {
        //code to save exceptions
    }
}

How can I set multiple global exception filters? I know that's possible to use "app.useGlobalFilters()" in "main.ts", but I need to use the dependency injection as I did in the first filter:

@Module({
    imports: [FgtsModule, MteModule],
    providers: [
        {
            provide: APP_FILTER,
            useClass: AllExceptionsFilter,
        },
    ],
})
export class AppModule {}

So basically, if a puppeteer exception is thrown I need to use my puppeteer filter to save a specific log file and after "allExceptionsFilter" need to handle this same exception and sending back a friendly response to client.

0

There are 0 best solutions below