0
0
NestJSframework~10 mins

Global exception filters in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Global exception filters
Request received
Controller or Service executes
Exception thrown?
NoSend normal response
Yes
Global Exception Filter catches
Process exception (log, format)
Send error response to client
When a request causes an error, the global exception filter catches it, processes it, and sends a formatted error response.
Execution Sample
NestJS
import { Catch, ExceptionFilter, ArgumentsHost } from '@nestjs/common';

@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
  catch(exception: any, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    response.status(500).json({ message: 'Error occurred' });
  }
}
This code defines a global exception filter that catches all exceptions and sends a 500 error response with a message.
Execution Table
StepActionException Thrown?Filter Catches?Response Sent
1Request received and controller method calledNoNoNormal response sent
2Controller throws an exceptionYesYesFilter catches exception
3Filter processes exceptionYesYesSends JSON error response with status 500
4Request endsNoNoResponse sent to client
💡 Execution stops after sending error response or normal response to client
Variable Tracker
VariableStartAfter ExceptionAfter FilterFinal
exceptionundefinedError objectHandled by filterHandled
response.statusCode200200500500
response.bodyNormal dataNot set{"message":"Error occurred"}{"message":"Error occurred"}
Key Moments - 3 Insights
Why does the filter catch exceptions thrown anywhere in the app?
Because it is registered globally, it listens for exceptions from all controllers and services, as shown in execution_table step 2 and 3.
What happens if no exception is thrown?
The filter does not run and the normal response is sent, as shown in execution_table step 1.
How does the filter change the HTTP response?
It sets the status code to 500 and sends a JSON error message, as tracked in variable_tracker for response.statusCode and response.body.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the filter catch the exception?
AStep 2
BStep 1
CStep 4
DStep 3
💡 Hint
Check the 'Filter Catches?' column in execution_table rows
According to variable_tracker, what is the response status code after the filter processes the exception?
A200
B404
C500
D400
💡 Hint
Look at response.statusCode after filter in variable_tracker
If the controller does not throw an exception, what response is sent?
AError response with status 500
BNormal response
CNo response
DException filter response
💡 Hint
See execution_table step 1 where no exception is thrown
Concept Snapshot
Global Exception Filters in NestJS:
- Catch exceptions thrown anywhere in app
- Registered globally to handle all errors
- Modify HTTP response (status, body)
- Send formatted error messages
- Keeps error handling centralized and consistent
Full Transcript
Global exception filters in NestJS catch errors thrown during request handling anywhere in the app. When a controller or service throws an exception, the global filter catches it, processes it by setting the HTTP status code and response body, then sends a formatted error response to the client. If no exception occurs, the filter does not run and the normal response is sent. This centralizes error handling and keeps responses consistent.