0
0
NestJSframework~20 mins

Exception filters in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exception Filter Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when a custom exception filter catches an HttpException?

Consider a NestJS controller that throws an HttpException. A custom exception filter catches it and modifies the response. What will the client receive?

NestJS
import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common';

@Catch(HttpException)
export class CustomFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    const status = exception.getStatus();
    response.status(status).json({
      statusCode: status,
      message: 'Filtered: ' + exception.message,
    });
  }
}

// Controller method example:
// throw new HttpException('Original error', 400);
ANo response sent, request hangs
B{"statusCode":400,"message":"Original error"}
C500 Internal Server Error with default message
D{"statusCode":400,"message":"Filtered: Original error"}
Attempts:
2 left
💡 Hint

Think about how the filter modifies the response JSON before sending it.

📝 Syntax
intermediate
2:00remaining
Which option correctly implements an exception filter for all exceptions?

Choose the code snippet that correctly implements a NestJS exception filter catching all exceptions.

A
import { ExceptionFilter, Catch, ArgumentsHost } from '@nestjs/common';

@Catch(null)
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' });
  }
}
B
import { ExceptionFilter, Catch, ArgumentsHost } from '@nestjs/common';

@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
  catch(exception: unknown, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    response.status(500).json({ message: 'Error occurred' });
  }
}
C
import { ExceptionFilter, Catch, ArgumentsHost } from '@nestjs/common';

@Catch(Object)
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' });
  }
}
D
import { ExceptionFilter, Catch, ArgumentsHost } from '@nestjs/common';

@Catch(Error)
export class AllExceptionsFilter implements ExceptionFilter {
  catch(exception: Error, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    response.status(500).json({ message: 'Error occurred' });
  }
}
Attempts:
2 left
💡 Hint

Recall how to catch all exceptions regardless of type.

🔧 Debug
advanced
2:00remaining
Why does this exception filter not catch exceptions thrown in async methods?

Given this exception filter, exceptions thrown inside async controller methods are not caught. What is the likely cause?

NestJS
import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common';

@Catch(HttpException)
export class AsyncFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    response.status(exception.getStatus()).json({
      message: 'Caught async error',
    });
  }
}

// Controller method example:
// async someMethod() {
//   throw new HttpException('Async error', 400);
// }
AThe filter must implement an async catch method to handle async exceptions.
BThe filter only catches synchronous exceptions; async exceptions must be caught with try/catch.
CThe filter is not bound globally or to the controller, so it doesn't catch async exceptions.
DThe filter's @Catch decorator only catches exceptions thrown outside async methods.
Attempts:
2 left
💡 Hint

Think about how NestJS applies filters and their scope.

state_output
advanced
2:00remaining
What is the response status code when a filter catches a non-HttpException error?

Given a filter that only catches HttpException, what happens if a TypeError is thrown in a controller?

NestJS
import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common';

@Catch(HttpException)
export class HttpOnlyFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    response.status(exception.getStatus()).json({
      message: 'Http error caught',
    });
  }
}

// Controller method example:
// throw new TypeError('Type error');
A500 Internal Server Error with default NestJS error response
B200 OK with empty body
C400 Bad Request with 'Http error caught' message
DNo response sent, request hangs
Attempts:
2 left
💡 Hint

Consider what happens when an exception filter does not catch the thrown error type.

🧠 Conceptual
expert
2:00remaining
How does NestJS determine which exception filter to use when multiple filters apply?

In a NestJS app, multiple exception filters are applied globally, at controller level, and at method level. Which filter will handle an exception?

AThe most specific filter closest to the source of the exception (method > controller > global) handles it.
BThe first registered global filter always handles all exceptions regardless of other filters.
CFilters are merged and all handle the exception in order of registration.
DNestJS randomly picks one filter to handle the exception.
Attempts:
2 left
💡 Hint

Think about filter priority and scope in NestJS.