0
0
NestJSframework~20 mins

Catch decorator in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Catch Decorator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when an exception is caught by a Catch decorator?

Consider a NestJS filter using the @Catch() decorator that catches HttpException. What will be the response status code if the filter catches an exception with status 404?

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

@Catch(HttpException)
export class NotFoundFilter 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: 'Resource not found',
    });
  }
}
AThe response status code will be 404 with message 'Resource not found'.
BThe response status code will be 200 with message 'OK'.
CThe response status code will be 500 with message 'Internal server error'.
DThe response status code will be 400 with message 'Bad request'.
Attempts:
2 left
💡 Hint

Think about what exception.getStatus() returns for an HttpException.

📝 Syntax
intermediate
2:00remaining
Which option correctly applies the Catch decorator to catch multiple exception types?

In NestJS, how do you correctly use the @Catch() decorator to catch both HttpException and TypeError exceptions?

A
@Catch(HttpException, TypeError)
export class MultiCatchFilter implements ExceptionFilter { ... }
B
@Catch(HttpException || TypeError)
export class MultiCatchFilter implements ExceptionFilter { ... }
C
@Catch({HttpException, TypeError})
export class MultiCatchFilter implements ExceptionFilter { ... }
D
@Catch([HttpException, TypeError])
export class MultiCatchFilter implements ExceptionFilter { ... }
Attempts:
2 left
💡 Hint

Remember the decorator expects an array for multiple exception types.

🔧 Debug
advanced
2:00remaining
Why does this Catch decorator not catch exceptions as expected?

Given this filter, why does it fail to catch HttpException errors?

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

@Catch()
export class EmptyCatchFilter implements ExceptionFilter {
  catch(exception: any, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    response.status(500).json({ message: 'Caught an error' });
  }
}
ABecause the catch method is missing the exception type parameter.
BBecause @Catch() with no arguments does not catch any exceptions.
CBecause the filter does not call next() to continue the pipeline.
DBecause the response object is not properly retrieved from ArgumentsHost.
Attempts:
2 left
💡 Hint

Check what happens when @Catch() has no parameters.

state_output
advanced
2:00remaining
What is the JSON response output of this Catch filter?

What JSON response will be sent when this filter catches a BadRequestException?

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

@Catch(BadRequestException)
export class BadRequestFilter implements ExceptionFilter {
  catch(exception: BadRequestException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    const status = exception.getStatus();
    response.status(status).json({
      error: 'Bad Request',
      details: exception.message,
    });
  }
}
A{"statusCode":400,"message":"Bad Request Exception"}
B{"error":"Bad Request","details":["Bad Request Exception"]}
C{"error":"Bad Request","details":"Bad Request Exception"}
D{"error":"Bad Request","details":null}
Attempts:
2 left
💡 Hint

Check how exception.message is used in the JSON response.

🧠 Conceptual
expert
2:00remaining
Which statement about the Catch decorator's behavior is true?

Choose the correct statement about how the @Catch() decorator works in NestJS exception filters.

AThe <code>@Catch()</code> decorator can catch exceptions thrown both synchronously and asynchronously in the request lifecycle.
BThe <code>@Catch()</code> decorator can only catch exceptions thrown synchronously inside controllers.
CThe <code>@Catch()</code> decorator automatically logs all caught exceptions to the console.
DThe <code>@Catch()</code> decorator must be used on every controller method to catch exceptions.
Attempts:
2 left
💡 Hint

Think about how NestJS handles exceptions in async code.