What is the purpose of an Exception Filter in NestJS?
An Exception Filter in NestJS catches errors thrown during request handling and allows you to control the response sent back to the client, improving error management.
Click to reveal answer
beginner
How do you create a custom Exception Filter in NestJS?
You create a class that implements the <code>ExceptionFilter</code> interface and define a <code>catch()</code> method to handle exceptions and send custom responses.
Click to reveal answer
beginner
Which decorator is used to bind an Exception Filter to a controller or route in NestJS?
The @UseFilters() decorator is used to apply an Exception Filter to a controller or specific route handler.
Click to reveal answer
intermediate
What is the role of the ArgumentsHost parameter in the catch() method of an Exception Filter?
ArgumentsHost provides access to the underlying platform's request and response objects, allowing you to customize the error response based on the context (HTTP, WebSocket, etc.).
Click to reveal answer
intermediate
Can Exception Filters in NestJS handle both synchronous and asynchronous exceptions?
Yes, Exception Filters can catch exceptions thrown synchronously or asynchronously during request processing, making error handling consistent.
Click to reveal answer
Which interface must a custom Exception Filter implement in NestJS?
AFilterInterface
BErrorHandler
CCatchable
DExceptionFilter
✗ Incorrect
Custom Exception Filters must implement the ExceptionFilter interface to define the catch() method.
What decorator applies an Exception Filter to a specific route or controller?
A@UseFilters()
B@HandleError()
C@Exception()
D@Catch()
✗ Incorrect
@UseFilters() binds Exception Filters to controllers or routes.
What does the catch() method receive as arguments?
ARequest and Response objects
BException object and ArgumentsHost
COnly the Exception object
DOnly the ArgumentsHost
✗ Incorrect
The catch() method receives the thrown exception and an ArgumentsHost to access request/response.
Which of these is NOT a typical use of Exception Filters?
AHandling database migrations
BLogging errors
CTransforming error responses
DSending custom HTTP status codes
✗ Incorrect
Exception Filters do not handle database migrations; they focus on error handling.
Can Exception Filters handle exceptions from asynchronous code in NestJS?
AOnly exceptions from Promises
BNo, only synchronous exceptions
CYes, they handle both sync and async exceptions
DOnly exceptions from Observables
✗ Incorrect
Exception Filters catch exceptions thrown synchronously or asynchronously.
Explain how to create and apply a custom Exception Filter in NestJS.
Think about the class, method, and decorator involved.
You got /4 concepts.
Describe the benefits of using Exception Filters in a NestJS application.
Consider how filters improve error management and user experience.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of an exception filter in NestJS?
easy
A. To catch and handle errors in a centralized way
B. To create database connections automatically
C. To define routes for HTTP requests
D. To manage user authentication
Solution
Step 1: Understand what exception filters do
Exception filters catch errors thrown during request handling and allow custom responses.
Step 2: Compare with other options
Options B, C, and D relate to other NestJS features, not error handling.
Final Answer:
To catch and handle errors in a centralized way -> Option A
C. Using exception.message instead of exception.getMessage()
D. Not importing HttpException class
Solution
Step 1: Check the @Catch() decorator usage
The @Catch(HttpException) references HttpException, but it is not imported from '@nestjs/common'.
Step 2: Validate other parts
catch method can omit return type and exception type in loose TS, exception.message is valid.
Final Answer:
Not importing HttpException class -> Option D
Quick Check:
Import HttpException for @Catch(HttpException) [OK]
Hint: Import exception classes used in @Catch() [OK]
Common Mistakes:
Not importing referenced exception types like HttpException
Assuming catch method needs strict return type
Thinking exception needs getMessage() method
5. You want to create a global exception filter that catches all exceptions and logs the error before sending a generic message. Which of these is the correct way to apply it globally in NestJS?
hard
A. Register the filter in a module's providers array only
B. Add @Global() decorator to the filter class
C. Use app.useGlobalFilters(new YourFilter()) in main.ts after creating the filter class
D. Call app.setGlobalExceptionFilter(YourFilter) in main.ts
Solution
Step 1: Recall how to apply global filters in NestJS
Global filters are applied using app.useGlobalFilters() method in the main bootstrap file.
Step 2: Evaluate other options
@Global() is for modules, not filters; registering in providers is local; app.setGlobalExceptionFilter() does not exist.
Final Answer:
Use app.useGlobalFilters(new YourFilter()) in main.ts after creating the filter class -> Option C
Quick Check:
Global filters use app.useGlobalFilters() [OK]
Hint: Global filters need app.useGlobalFilters() call [OK]
Common Mistakes:
Using @Global() decorator on filter class
Registering filter only in providers without global call
Assuming app.setGlobalExceptionFilter() exists
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.