📖 Scenario: You are building a NestJS backend for a bookstore. You want to handle errors clearly by creating custom exception classes for specific error cases like when a book is not found or when a user tries to buy a book that is out of stock.
🎯 Goal: Create custom exception classes in NestJS by extending the built-in HttpException class. Use these classes to throw meaningful errors in your application.
📋 What You'll Learn
Create a custom exception class called BookNotFoundException that extends HttpException.
Create a custom exception class called OutOfStockException that extends HttpException.
Use the HttpStatus.NOT_FOUND status code for BookNotFoundException.
Use the HttpStatus.BAD_REQUEST status code for OutOfStockException.
Each exception class should have a constructor that calls the parent HttpException constructor with a custom message and status code.
💡 Why This Matters
🌍 Real World
Custom exceptions help backend developers give clear error messages and HTTP status codes. This improves API usability and debugging.
💼 Career
Understanding how to create and use custom exceptions is important for backend developers working with NestJS or similar frameworks to build robust and maintainable APIs.
Progress0 / 4 steps
1
Create the BookNotFoundException class
Create a class called BookNotFoundException that extends HttpException. Inside the constructor, call super with the message 'Book not found' and the status code HttpStatus.NOT_FOUND. Import HttpException and HttpStatus from @nestjs/common.
NestJS
Hint
Remember to import HttpException and HttpStatus from @nestjs/common. Your class should extend HttpException and call super with the message and status code.
2
Create the OutOfStockException class
Create a class called OutOfStockException that extends HttpException. Inside the constructor, call super with the message 'Book is out of stock' and the status code HttpStatus.BAD_REQUEST. Make sure to export the class.
NestJS
Hint
Follow the same pattern as BookNotFoundException. Use the message 'Book is out of stock' and status code HttpStatus.BAD_REQUEST.
3
Throw BookNotFoundException in a service method
Inside a class called BookService, create a method called findBookById that takes a parameter id. Inside the method, throw a new BookNotFoundException. Import BookNotFoundException from the current file.
NestJS
Hint
Create the BookService class and add the findBookById method. Inside it, throw new BookNotFoundException().
4
Throw OutOfStockException in a service method
Inside the BookService class, create a method called buyBook that takes a parameter id. Inside the method, throw a new OutOfStockException. Make sure to keep the previous code intact.
NestJS
Hint
Add the buyBook method inside BookService and throw new OutOfStockException() inside it.
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?