Discover how one simple filter can save you from endless error-handling headaches!
Why Global exception filters in NestJS? - Purpose & Use Cases
Imagine building a NestJS app where every route handler has its own try-catch block to handle errors.
You have to repeat error handling code everywhere, making your code messy and hard to maintain.
Manually catching errors in every place is tiring and easy to forget.
It leads to inconsistent error responses and scattered error logic.
Debugging becomes harder because error handling is not centralized.
Global exception filters let you catch and handle all errors in one place.
This keeps your code clean and ensures consistent error responses across your app.
try { /* handler code */ } catch (e) { return { status: 500, message: 'Error' }; }
app.useGlobalFilters(new GlobalExceptionFilter()); // in main.ts, centralized error handlingYou can manage all errors in one spot, making your app more reliable and easier to maintain.
In a real app, a global filter can catch database errors or validation failures and send friendly messages to users without repeating code.
Manual error handling is repetitive and error-prone.
Global exception filters centralize error management.
This leads to cleaner code and consistent error responses.
Practice
Solution
Step 1: Understand the role of exception filters
Exception filters in NestJS are designed to catch errors and exceptions that occur during request processing.Step 2: Identify the scope of global exception filters
Global exception filters apply this error handling across the entire app, centralizing error management.Final Answer:
To catch and handle exceptions across the entire application in one place -> Option AQuick Check:
Global exception filters = app-wide error handling [OK]
- Confusing filters with routing or database management
- Thinking filters serve static files
- Assuming filters only handle specific routes
main.ts file of a NestJS app?Solution
Step 1: Recall the NestJS method for global filters
The official method to apply global exception filters isuseGlobalFilters()on the app instance.Step 2: Check the syntax for passing the filter
The filter must be instantiated withnewbefore passing it touseGlobalFilters().Final Answer:
app.useGlobalFilters(new MyExceptionFilter()) -> Option AQuick Check:
Global filter method = useGlobalFilters() [OK]
- Using incorrect method names like setGlobalFilters
- Passing class without instantiation
- Confusing with middleware registration methods
import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common';
@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
catch(exception: unknown, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
if (exception instanceof HttpException) {
const status = exception.getStatus();
response.status(status).json({ message: exception.message });
} else {
response.status(500).json({ message: 'Internal server error' });
}
}
}Solution
Step 1: Analyze the filter's catch method
The filter checks if the exception is an instance of HttpException and uses its status code.Step 2: Determine status code for other exceptions
If not an HttpException, it returns status 500 with a generic error message.Final Answer:
Depends on the exception type -> Option CQuick Check:
HttpException status or 500 for others [OK]
- Assuming always 500 status
- Ignoring HttpException status extraction
- Confusing 404 with 500 errors
import { ExceptionFilter, Catch, ArgumentsHost } from '@nestjs/common';
@Catch()
export class MyFilter implements ExceptionFilter {
catch(exception: any, host: ArgumentsHost) {
const response = host.getResponse();
response.status(400).json({ error: 'Bad Request' });
}
}Solution
Step 1: Check how to get response object
In NestJS, to get the HTTP response, usehost.switchToHttp().getResponse(), nothost.getResponse().Step 2: Verify other parts
The @Catch() decorator can be empty to catch all exceptions, and the catch method does not need to return a value. @Injectable() is not required for filters.Final Answer:
host.getResponse() is incorrect; should use host.switchToHttp().getResponse() -> Option DQuick Check:
Use switchToHttp() to access response [OK]
- Calling host.getResponse() directly
- Thinking @Injectable() is mandatory for filters
- Expecting catch to return a value
catch method?Solution
Step 1: Handle HttpException separately
Check if the exception is an instance of HttpException to preserve its status and message.Step 2: For other exceptions, log and respond with status 418
Log the error details, then send a JSON response with status 418 and a custom message.Final Answer:
Check if exception is HttpException, use its status; else log and respond with 418 and custom message -> Option BQuick Check:
Preserve HttpException status, else respond 418 with log [OK]
- Ignoring HttpException status and always sending 418
- Not logging exceptions
- Re-throwing exceptions instead of handling
Ready to prove it?
Pass the test and this pattern moves unknown
