Challenge - 5 Problems
Global Exception Filter Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when a global exception filter catches an HTTP exception?
Consider a NestJS app with a global exception filter that catches all exceptions and returns a JSON with
{ status: 'error', message: error.message }. What will the client receive if a controller throws new HttpException('Not Found', 404)?NestJS
import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common'; @Catch() export class GlobalFilter implements ExceptionFilter { catch(exception: any, host: ArgumentsHost) { const ctx = host.switchToHttp(); const response = ctx.getResponse(); const message = exception.message || 'Unknown error'; response.status(500).json({ status: 'error', message }); } } // Controller throws: throw new HttpException('Not Found', 404);
Attempts:
2 left
💡 Hint
The filter catches all exceptions and uses exception.message for the response message.
✗ Incorrect
The global filter catches the HttpException, extracts its message 'Not Found', and returns it with status 500 in JSON format. So the client sees the JSON with status 'error' and message 'Not Found'.
❓ lifecycle
intermediate1:30remaining
When is a global exception filter executed in NestJS request lifecycle?
In NestJS, at what point does a global exception filter run during handling a request?
Attempts:
2 left
💡 Hint
Exception filters catch errors thrown during controller execution.
✗ Incorrect
Global exception filters catch exceptions thrown by controllers or services before the response is sent. They allow custom handling or formatting of errors.
🔧 Debug
advanced2:30remaining
Why does this global exception filter not catch exceptions?
Given this global exception filter code, why might it fail to catch exceptions thrown in controllers?
import { ExceptionFilter, Catch, ArgumentsHost } from '@nestjs/common';
@Catch(Error)
export class MyFilter implements ExceptionFilter {
catch(exception: any, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
response.status(500).json({ message: 'Error caught' });
}
}Attempts:
2 left
💡 Hint
Check how global filters are applied in NestJS.
✗ Incorrect
Defining a filter class alone does not activate it globally. It must be registered globally using app.useGlobalFilters(new MyFilter()) in main.ts.
📝 Syntax
advanced2:30remaining
Which option correctly implements a global exception filter that logs and rethrows exceptions?
Choose the correct global exception filter code that logs the exception message and then rethrows it to let NestJS handle it.
Attempts:
2 left
💡 Hint
To rethrow an exception, use throw inside catch method.
✗ Incorrect
Option A logs the message and rethrows the exception, allowing NestJS to handle it further. Other options either swallow the exception or use invalid syntax.
🧠 Conceptual
expert2:00remaining
What is the main benefit of using a global exception filter in NestJS?
Why would a developer choose to implement a global exception filter instead of handling exceptions individually in each controller?
Attempts:
2 left
💡 Hint
Think about code reuse and consistency in user experience.
✗ Incorrect
Global exception filters let developers write error handling once and apply it everywhere, ensuring consistent responses and easier maintenance.