Challenge - 5 Problems
NestJS Exception 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 custom exception is thrown in NestJS?
Consider this custom exception class in NestJS:
If this exception is thrown inside a controller method, what HTTP status code and message will the client receive?
import { HttpException, HttpStatus } from '@nestjs/common';
export class MyCustomException extends HttpException {
constructor() {
super('Custom error occurred', HttpStatus.BAD_REQUEST);
}
}If this exception is thrown inside a controller method, what HTTP status code and message will the client receive?
Attempts:
2 left
💡 Hint
Remember that HttpException allows setting both message and status code.
✗ Incorrect
The custom exception extends HttpException with status BAD_REQUEST (400) and a custom message. NestJS sends this status and message to the client.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a custom exception class in NestJS?
You want to create a custom exception class named `UnauthorizedException` that returns status 401 with message 'Access denied'. Which code snippet is correct?
Attempts:
2 left
💡 Hint
HttpException constructor takes message first, then status code.
✗ Incorrect
Option B correctly extends HttpException and calls super with message then status code. Other options either misuse parameters or don't extend HttpException.
🔧 Debug
advanced2:00remaining
Why does this custom exception not return the expected status code?
Given this code:
When thrown, the client receives status 500 instead of 403. Why?
export class MyException extends HttpException {
constructor() {
super(HttpStatus.FORBIDDEN, 'Forbidden access');
}
}When thrown, the client receives status 500 instead of 403. Why?
Attempts:
2 left
💡 Hint
Check the order of arguments in HttpException constructor.
✗ Incorrect
HttpException expects message first, then status code. Passing status code first causes unexpected behavior and defaults to 500.
❓ state_output
advanced2:00remaining
What is the response body when throwing a custom exception with a response object?
Consider this custom exception:
What will the client receive as the JSON response body when this exception is thrown?
export class ValidationException extends HttpException {
constructor(errors: string[]) {
super({ message: 'Validation failed', errors }, HttpStatus.UNPROCESSABLE_ENTITY);
}
}What will the client receive as the JSON response body when this exception is thrown?
Attempts:
2 left
💡 Hint
Passing an object as the first argument to HttpException sets the response body.
✗ Incorrect
When an object is passed as the first argument, NestJS uses it as the JSON response body exactly.
🧠 Conceptual
expert3:00remaining
Which statement about custom exceptions in NestJS is TRUE?
Select the correct statement about creating and using custom exception classes in NestJS.
Attempts:
2 left
💡 Hint
Think about how NestJS handles exceptions and status codes.
✗ Incorrect
Only exceptions extending HttpException and calling super properly set the HTTP response status and message. Other options are incorrect.