Consider a NestJS filter using the @Catch() decorator that catches HttpException. What will be the response status code if the filter catches an exception with status 404?
import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common'; @Catch(HttpException) export class NotFoundFilter 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: 'Resource not found', }); } }
Think about what exception.getStatus() returns for an HttpException.
The @Catch(HttpException) decorator catches exceptions of type HttpException. The getStatus() method returns the HTTP status code of the exception, which in this case is 404. The filter sets the response status to this code and sends a JSON message.
In NestJS, how do you correctly use the @Catch() decorator to catch both HttpException and TypeError exceptions?
Remember the decorator expects an array for multiple exception types.
The @Catch() decorator accepts either a single exception class or an array of exception classes. To catch multiple exceptions, you must pass an array like @Catch([HttpException, TypeError]). Other syntaxes are invalid.
Given this filter, why does it fail to catch HttpException errors?
import { ExceptionFilter, Catch, ArgumentsHost } from '@nestjs/common'; @Catch() export class EmptyCatchFilter implements ExceptionFilter { catch(exception: any, host: ArgumentsHost) { const ctx = host.switchToHttp(); const response = ctx.getResponse(); response.status(500).json({ message: 'Caught an error' }); } }
Check what happens when @Catch() has no parameters.
The @Catch() decorator requires at least one exception type to catch. If no arguments are provided, the filter will not catch any exceptions. To catch all exceptions, you must use @Catch() with no parameters only if you implement a global filter, but in this context, it does not catch anything.
What JSON response will be sent when this filter catches a BadRequestException?
import { ExceptionFilter, Catch, ArgumentsHost, BadRequestException } from '@nestjs/common'; @Catch(BadRequestException) export class BadRequestFilter implements ExceptionFilter { catch(exception: BadRequestException, host: ArgumentsHost) { const ctx = host.switchToHttp(); const response = ctx.getResponse(); const status = exception.getStatus(); response.status(status).json({ error: 'Bad Request', details: exception.message, }); } }
Check how exception.message is used in the JSON response.
The filter sends a JSON with keys error and details. The details field contains the exception's message string. So the output is a JSON string with those keys and the message as a string.
Choose the correct statement about how the @Catch() decorator works in NestJS exception filters.
Think about how NestJS handles exceptions in async code.
The @Catch() decorator applies to exception filters that catch exceptions thrown anywhere in the request lifecycle, including asynchronous operations. It does not automatically log exceptions, nor must it be applied on every method. It works globally or scoped to controllers/providers.