0
0
NestJSframework~5 mins

Catch decorator in NestJS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @Catch decorator in NestJS?
The @Catch decorator is used to define an exception filter that handles specific exceptions thrown in the application, allowing centralized error handling.
Click to reveal answer
beginner
How do you specify which exceptions a filter should catch using the @Catch decorator?
You pass one or more exception classes as arguments to the @Catch decorator. The filter will only handle those exceptions.
Click to reveal answer
intermediate
What method must be implemented in a class decorated with <code>@Catch</code> to handle exceptions?
The class must implement the <code>catch(exception: any, host: ArgumentsHost)</code> method, which contains the logic to handle the caught exception.
Click to reveal answer
intermediate
Can the @Catch decorator be used without arguments? What does that mean?
Yes, if used without arguments, the filter will catch all exceptions, acting as a global catch-all error handler.
Click to reveal answer
intermediate
How do you apply a class with the <code>@Catch</code> decorator globally in a NestJS application?
You provide the filter class in the <code>app.useGlobalFilters(new YourFilter())</code> method inside the main bootstrap function.
Click to reveal answer
What does the @Catch(HttpException) decorator do?
ACatches all exceptions
BDoes nothing without a catch method
CCatches exceptions except HttpException
DCatches only HttpException errors
Which method handles the exception inside a class decorated with @Catch?
Acatch()
Bhandle()
Cprocess()
Dexecute()
If you want to catch all exceptions, how do you use the @Catch decorator?
A<code>@Catch(null)</code>
B<code>@Catch(AllExceptions)</code>
C<code>@Catch()</code> with no arguments
D<code>@Catch(Error)</code>
How do you register a catch filter globally in NestJS?
AUse <code>app.useGlobalFilters()</code>
BAdd it to <code>providers</code> in a module
CUse <code>app.use()</code>
DAdd it to <code>imports</code> in a module
What argument does the catch() method receive besides the exception?
ARequest object
BArgumentsHost object
CResponse object
DNext function
Explain how the @Catch decorator works in NestJS and how you create a custom exception filter.
Think about how you tell NestJS which errors to handle and where you write the handling code.
You got /4 concepts.
    Describe the steps to apply a custom exception filter globally in a NestJS application.
    Focus on how to make the filter work for the whole app, not just one controller.
    You got /4 concepts.