A custom exception class in NestJS is a user-defined class that extends the built-in HttpException class to create specific error types with custom messages and HTTP status codes.
Click to reveal answer
beginner
How do you create a custom exception class in NestJS?
You create a custom exception class by extending the HttpException class and calling its constructor with a message and an HTTP status code.
Click to reveal answer
intermediate
Why use custom exception classes instead of throwing generic errors?
Custom exception classes provide clear, consistent error responses with specific HTTP status codes and messages, making error handling and debugging easier and improving API clarity.
Click to reveal answer
beginner
Example: What does this custom exception class do?
class ForbiddenException extends HttpException {
constructor() {
super('Access denied', HttpStatus.FORBIDDEN);
}
}
This class creates a custom exception named ForbiddenException that returns a 403 Forbidden HTTP status with the message 'Access denied' when thrown.
Click to reveal answer
intermediate
How does NestJS handle custom exceptions when thrown in a controller?
NestJS catches custom exceptions thrown in controllers and automatically sends an HTTP response with the status code and message defined in the exception class.
Click to reveal answer
Which class should you extend to create a custom exception in NestJS?
AError
BHttpException
CExceptionFilter
DController
✗ Incorrect
Custom exceptions in NestJS extend the HttpException class to leverage built-in HTTP error handling.
What parameters does the HttpException constructor expect?
AOnly an HTTP status code
BOnly a message
CA message and an HTTP status code
DA request and response object
✗ Incorrect
HttpException constructor requires a message and an HTTP status code to define the error response.
What HTTP status code does HttpStatus.FORBIDDEN represent?
A401
B500
C404
D403
✗ Incorrect
HttpStatus.FORBIDDEN corresponds to the 403 status code, meaning access is denied.
Why is it better to use custom exceptions instead of generic errors in NestJS?
AThey provide specific HTTP status codes and messages
BThey run faster
CThey require less code
DThey automatically fix bugs
✗ Incorrect
Custom exceptions improve clarity by sending specific HTTP status codes and messages.
What happens when a custom exception is thrown inside a NestJS controller?
ANestJS sends an HTTP response with the exception's status and message
BThe server crashes
CNothing happens
DThe exception is ignored
✗ Incorrect
NestJS catches the exception and sends an HTTP response with the defined status and message.
Explain how to create and use a custom exception class in NestJS.
Think about extending the built-in error class and how NestJS handles thrown exceptions.
You got /4 concepts.
Why are custom exception classes useful in building APIs with NestJS?
Consider how clients and developers benefit from clear and consistent error responses.
You got /4 concepts.
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
Step 1: Understand the role of custom exceptions
Custom exceptions in NestJS help you send clear, specific error messages with HTTP status codes.
Step 2: Compare with other options
Options B, C, and D describe unrelated features not handled by custom exceptions.
Final Answer:
To define specific error messages and HTTP status codes for your app -> Option D
Quick Check:
Custom exceptions = specific messages + status [OK]
Hint: Custom exceptions customize error messages and HTTP codes [OK]
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
Step 1: Check syntax for custom exception
In NestJS, custom exceptions extend HttpException and call super with message and status.
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.
Final Answer:
class MyException extends HttpException { constructor() { super('Error', 400); } } -> Option B
The super() call only has a message string, but HttpException requires message and status code.
Step 2: Understand HttpException constructor
HttpException constructor needs both message and HTTP status code to work correctly.
Final Answer:
Missing HTTP status code in super() call -> Option A
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?