0
0
NestJSframework~30 mins

Custom exception classes in NestJS - Mini Project: Build & Apply

Choose your learning style9 modes available
Custom Exception Classes in NestJS
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Add the buyBook method inside BookService and throw new OutOfStockException() inside it.