0
0
NestJSframework~20 mins

Custom exception classes in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS Exception 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 a custom exception is thrown in NestJS?
Consider this custom exception class in NestJS:
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?
AStatus code 400 with message 'Custom error occurred'
BStatus code 500 with message 'Internal server error'
CStatus code 404 with message 'Not Found'
DStatus code 200 with message 'OK'
Attempts:
2 left
💡 Hint
Remember that HttpException allows setting both message and status code.
📝 Syntax
intermediate
2: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?
A
export class UnauthorizedException extends Error {
  constructor() {
    super('Access denied');
    this.status = 401;
  }
}
B
import { HttpException, HttpStatus } from '@nestjs/common';

export class UnauthorizedException extends HttpException {
  constructor() {
    super('Access denied', HttpStatus.UNAUTHORIZED);
  }
}
C
export class UnauthorizedException extends HttpException {
  constructor() {
    super(HttpStatus.UNAUTHORIZED, 'Access denied');
  }
}
D
export class UnauthorizedException {
  constructor() {
    this.message = 'Access denied';
    this.status = 401;
  }
}
Attempts:
2 left
💡 Hint
HttpException constructor takes message first, then status code.
🔧 Debug
advanced
2:00remaining
Why does this custom exception not return the expected status code?
Given this code:
export class MyException extends HttpException {
  constructor() {
    super(HttpStatus.FORBIDDEN, 'Forbidden access');
  }
}

When thrown, the client receives status 500 instead of 403. Why?
AThe parameters to super() are reversed; message should come first, then status code.
BHttpStatus.FORBIDDEN is not a valid status code.
CThe class must implement an interface to set status code.
DNestJS does not support custom exceptions.
Attempts:
2 left
💡 Hint
Check the order of arguments in HttpException constructor.
state_output
advanced
2:00remaining
What is the response body when throwing a custom exception with a response object?
Consider this custom exception:
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?
A"Validation failed"
B{"error":"Unprocessable Entity"}
C{"statusCode":422,"message":"Validation failed"}
D{"message":"Validation failed","errors":["error1","error2"]}
Attempts:
2 left
💡 Hint
Passing an object as the first argument to HttpException sets the response body.
🧠 Conceptual
expert
3:00remaining
Which statement about custom exceptions in NestJS is TRUE?
Select the correct statement about creating and using custom exception classes in NestJS.
ANestJS requires custom exceptions to be registered in a module before use.
BCustom exceptions can extend any class and NestJS will automatically detect the status code from the class name.
CCustom exceptions must extend HttpException and call super with message then status code to set HTTP response correctly.
DCustom exceptions do not need to call super() if they implement the IException interface.
Attempts:
2 left
💡 Hint
Think about how NestJS handles exceptions and status codes.