0
0
NestJSframework~10 mins

Exception filters in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Exception filters
Request received
Controller method runs
Exception thrown?
NoSend response
Yes
Exception caught by filter
Filter handles exception
Send error response
When a request triggers an error, NestJS passes the error to an exception filter which decides how to respond.
Execution Sample
NestJS
import { ExceptionFilter, Catch, ArgumentsHost } from '@nestjs/common';

@Catch(Error)
export class MyFilter implements ExceptionFilter {
  catch(exception: Error, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    response.status(500).json({ message: exception.message });
  }
}
This filter catches all Error exceptions and sends a 500 JSON response with the error message.
Execution Table
StepActionException StateFilter CalledResponse Sent
1Request received, controller method startsNo exceptionNoNo
2Controller throws Error('Fail')Error('Fail') thrownYesNo
3Filter.catch called with Error('Fail')Error handledYesNo
4Filter sends response status 500 with JSON {message: 'Fail'}HandledYesYes
5Response sent to clientHandledNoYes
💡 Exception handled by filter and response sent, stopping normal flow
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
exceptionnullError('Fail')Error('Fail')HandledHandled
response.statusCodeundefinedundefinedundefined500500
response.bodyundefinedundefinedundefined{"message":"Fail"}{"message":"Fail"}
Key Moments - 2 Insights
Why does the filter catch the exception instead of the controller handling it?
Because the controller threw an exception, NestJS automatically passes it to the registered exception filter as shown in execution_table step 2 and 3.
What happens if the filter does not send a response?
The request would hang because the filter must send a response to end the request, as shown in step 4 where response.status and json are called.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the exception first caught by the filter?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Check the 'Filter Called' column in execution_table rows
According to variable_tracker, what is the response status code after step 3?
Aundefined
B200
C500
D404
💡 Hint
Look at 'response.statusCode' value after Step 3 in variable_tracker
If the controller did not throw an exception, what would happen according to the concept flow?
AFilter would still send a 500 response
BRequest would be handled normally and response sent
CRequest would hang waiting for filter
DFilter would throw an error
💡 Hint
Refer to the 'No exception' branch in concept_flow
Concept Snapshot
Exception filters catch errors thrown in controllers.
Use @Catch decorator to specify exceptions.
Implement catch() to handle and send response.
Filters stop normal flow by sending error responses.
Useful for custom error messages and status codes.
Full Transcript
In NestJS, when a controller method throws an exception, the framework passes it to an exception filter if one is registered. The filter's catch method receives the exception and the context, allowing it to send a custom response. This stops the normal request flow and returns an error response to the client. The example filter catches all Error exceptions and sends a 500 status with a JSON message. The execution table shows the request starting, the exception thrown, the filter catching it, and the response sent. Variables track the exception and response status and body changes. Key moments clarify why filters catch exceptions and the importance of sending a response. The quiz tests understanding of when the filter is called, response status changes, and normal flow without exceptions.