Bird
Raised Fist0
NestJSframework~20 mins

Custom exception classes in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of creating a custom exception class in NestJS?
easy
A. To create new HTTP methods like GET or POST
B. To replace the built-in JavaScript Error object globally
C. To automatically log errors to a database
D. To define specific error messages and HTTP status codes for your app

Solution

  1. Step 1: Understand the role of custom exceptions

    Custom exceptions in NestJS help you send clear, specific error messages with HTTP status codes.
  2. Step 2: Compare with other options

    Options B, C, and D describe unrelated features not handled by custom exceptions.
  3. Final Answer:

    To define specific error messages and HTTP status codes for your app -> Option D
  4. Quick Check:

    Custom exceptions = specific messages + status [OK]
Hint: Custom exceptions customize error messages and HTTP codes [OK]
Common Mistakes:
  • Thinking custom exceptions replace JavaScript Error globally
  • Assuming they log errors automatically
  • Confusing exceptions with HTTP methods
2. Which of the following is the correct way to define a custom exception class in NestJS?
easy
A. const MyException = () => { return 'Error'; }
B. class MyException extends HttpException { constructor() { super('Error', 400); } }
C. class MyException { constructor() { this.message = 'Error'; } }
D. function MyException() { throw new Error('Error'); }

Solution

  1. Step 1: Check syntax for custom exception

    In NestJS, custom exceptions extend HttpException and call super with message and status.
  2. Step 2: Validate options

    class MyException extends HttpException { constructor() { super('Error', 400); } } correctly extends HttpException and calls super with message and status code. Others do not follow NestJS pattern.
  3. Final Answer:

    class MyException extends HttpException { constructor() { super('Error', 400); } } -> Option B
  4. Quick Check:

    Extend HttpException + super(message, status) [OK]
Hint: Custom exceptions must extend HttpException and call super() [OK]
Common Mistakes:
  • Not extending HttpException
  • Not calling super() with message and status
  • Using plain functions instead of classes
3. Given this custom exception class, what will be the HTTP status code when it is thrown?
class ForbiddenException extends HttpException {
  constructor() {
    super('Access denied', 403);
  }
}
medium
A. 200
B. 500
C. 403
D. 404

Solution

  1. Step 1: Read the constructor call

    The constructor calls super with message 'Access denied' and status 403.
  2. Step 2: Understand HttpException behavior

    The second argument to super is the HTTP status code returned when exception is thrown.
  3. Final Answer:

    403 -> Option C
  4. Quick Check:

    Status code = 403 from super() call [OK]
Hint: Status code is second argument in super() call [OK]
Common Mistakes:
  • Confusing message with status code
  • Assuming default 500 status
  • Ignoring the status argument in super()
4. What is wrong with this custom exception class?
class BadRequestException extends HttpException {
  constructor() {
    super('Bad request');
  }
}
medium
A. Missing HTTP status code in super() call
B. Class should not extend HttpException
C. Constructor should not call super()
D. Message should be a number, not a string

Solution

  1. Step 1: Check super() arguments

    The super() call only has a message string, but HttpException requires message and status code.
  2. Step 2: Understand HttpException constructor

    HttpException constructor needs both message and HTTP status code to work correctly.
  3. Final Answer:

    Missing HTTP status code in super() call -> Option A
  4. Quick Check:

    super() needs message + status [OK]
Hint: Always pass status code as second argument to super() [OK]
Common Mistakes:
  • Forgetting the status code argument
  • Thinking message alone is enough
  • Misunderstanding HttpException constructor
5. You want to create a custom exception that returns a JSON response with a custom error code and message. Which approach correctly extends HttpException to achieve this?
class CustomErrorException extends HttpException {
  constructor() {
    super(
      { errorCode: 1234, message: 'Custom error' },
      400
    );
  }
}
hard
A. This code correctly sends a JSON object with errorCode and message and HTTP 400 status
B. HttpException cannot send JSON objects, only strings
C. The status code should be inside the JSON object, not a separate argument
D. Custom exceptions must not use objects in super(), only strings

Solution

  1. Step 1: Review HttpException constructor usage

    HttpException accepts either a string or an object as the first argument for the response body.
  2. Step 2: Confirm JSON response with status code

    Passing an object with errorCode and message as first argument and 400 as second is valid and sends JSON response with status 400.
  3. Final Answer:

    This code correctly sends a JSON object with errorCode and message and HTTP 400 status -> Option A
  4. Quick Check:

    HttpException supports object response + status [OK]
Hint: Pass object as first argument, status code as second in super() [OK]
Common Mistakes:
  • Thinking only strings allowed in super()
  • Putting status code inside JSON object
  • Believing custom exceptions can't send JSON
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard