Reputation: 3549
What is the diff b/w both types of filters. And when to use what? Please explain with any example
@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
}
@Catch()
export class AllExceptionsFilter extends BaseExceptionFilter {
}
Upvotes: 6
Views: 2190
Reputation: 70131
ExceptionFilter
is an interface that defines that the current class should implement the catch
method with the signature (exception: unknown, host: ArgumentHost)
.
BaseExceptionFilter
is a class already made in NestJS with a working catch
method. By using extend
you can add your own logic to catch
and then in the end of the implementation call super.catch(exception, host)
and let Nest take care of the rest from there.
The main difference of the two is how much of the logic do you want to write vs how much do you want to add in. If you are happy with how Nest already handles errors, and just want to add in the ability to log your errors, say to a database, then extends BaseExceptionFilter
is a good fit. However, if you don't care or how Nest's exception filter works by default, then implements ExceptionFilter
and writing your own custom logic is the way to go.
Upvotes: 8