Consider a NestJS controller that throws an HttpException. A custom exception filter catches it and modifies the response. What will the client receive?
import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common'; @Catch(HttpException) export class CustomFilter implements ExceptionFilter { catch(exception: HttpException, host: ArgumentsHost) { const ctx = host.switchToHttp(); const response = ctx.getResponse(); const status = exception.getStatus(); response.status(status).json({ statusCode: status, message: 'Filtered: ' + exception.message, }); } } // Controller method example: // throw new HttpException('Original error', 400);
Think about how the filter modifies the response JSON before sending it.
The custom filter catches the HttpException and sends a JSON response with a modified message prefix 'Filtered: '. So the client receives the modified message, not the original one.
Choose the code snippet that correctly implements a NestJS exception filter catching all exceptions.
Recall how to catch all exceptions regardless of type.
Using @Catch() with empty parentheses catches all exceptions. Other options specify types incorrectly or invalidly.
Given this exception filter, exceptions thrown inside async controller methods are not caught. What is the likely cause?
import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common'; @Catch(HttpException) export class AsyncFilter implements ExceptionFilter { catch(exception: HttpException, host: ArgumentsHost) { const ctx = host.switchToHttp(); const response = ctx.getResponse(); response.status(exception.getStatus()).json({ message: 'Caught async error', }); } } // Controller method example: // async someMethod() { // throw new HttpException('Async error', 400); // }
Think about how NestJS applies filters and their scope.
Exception filters catch exceptions thrown in the request lifecycle if they are properly bound globally or to the controller. If not bound, exceptions in async methods won't be caught by this filter.
Given a filter that only catches HttpException, what happens if a TypeError is thrown in a controller?
import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common'; @Catch(HttpException) export class HttpOnlyFilter implements ExceptionFilter { catch(exception: HttpException, host: ArgumentsHost) { const ctx = host.switchToHttp(); const response = ctx.getResponse(); response.status(exception.getStatus()).json({ message: 'Http error caught', }); } } // Controller method example: // throw new TypeError('Type error');
Consider what happens when an exception filter does not catch the thrown error type.
The filter only catches HttpException. A TypeError is unhandled by this filter, so NestJS returns a 500 Internal Server Error by default.
In a NestJS app, multiple exception filters are applied globally, at controller level, and at method level. Which filter will handle an exception?
Think about filter priority and scope in NestJS.
NestJS uses the closest filter to the exception source. Method-level filters override controller-level, which override global filters.