Skip to content

not-soo-techie/Error-Handling

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧩 Node.js Lab – Error Handling in Express

📘 Overview

In this lab, you will build a simple Product Management API using Node.js and Express that demonstrates proper error handling practices in backend development.

The objective is to help you understand how to:

  • Handle different types of runtime and operational errors in Express.
  • Structure error-handling middleware.
  • Validate and manage request data properly.
  • Work with asynchronous file operations using the Node.js fs/promises module.

🧠 Problem Statement

You are building a Product Management REST API that allows users to:

  • View all products
  • View a specific product by ID
  • Add a new product

The product data will be stored locally in a products.json file inside the data folder.

Your main focus is to implement proper error handling across the application.


🏗️ Project Structure

Your project should have the following folder layout:

Error-Handling/
│
├── controllers/
│   └── productController.js
├── data/ 
│   └── products.json          
├── middleware/            
│   └── errorHandler.js               
├── routes/
│   └── productRoutes.js                
├── src/
│   └── server.js                  
├── test/               
│   └── product.test.js
└── README.md                   

🧩 Tasks to Complete

1️⃣ Global Error Handling Middleware

Create a centralized error-handling middleware inside
/middleware/errorHandler.js that catches all errors in one place.

Example:

app.use((err, req, res, next) => {
  // Logic to handle errors globally
});

Error should be sent in JSON format like :

{  "error": "Internal Server Error" }

2️⃣ Route for Undefined Endpoints (404 Handler)

In your app.js, define a route handler for any undefined routes.

Example:

app.use((req, res) => {
  // Logic to handle undefined routes
});

Error should be sent in JSON format like :

{  "error": "Route not found" }

3️⃣ Controller: Get All Products

Inside controllers/productController.js, implement:

Example:

export const getAllProducts = async (req, res, next) => {
  try {
    // Read from products.json using fs/promises
    // Send the list of products as JSON
  } catch (err) {
    next(err); // Pass error to middleware
  }
};

Should return a list of all products in JSON format.

{  "products": products }

4️⃣ Controller: Get Product by ID

Implement:

Example:

export const getProductById = async (req, res, next) => {
    // Extract id from params
    // Find product by id
    // If not found, return 404 with { error: "Product not found" }
};
  • If product not found → respond with :
{  "error": "Product not found" }

and 404 Not Found status code.

  • On success → return:
{ "id": 1, "name": "Product Name", "price": 100 }

5️⃣ Controller: Add Product

Implement:

Example:

export const addProduct = async (req, res, next) => {
  try {
    // Validate name and price fields
    // Read existing products from file
    // Create new product object with auto-incremented ID
    // Write back to products.json
    // Return 201 Created with the new product
  } catch (err) {
    next(err);
  }
};
  • If validation fails → respond with :
{ "error": "Invalid product data" }

and 400 Bad Request status code.

  • On success → return:
{ "id": 1, "name": "Product Name", "price": 100 }

⚡ Error Types to Handle {#error-types-to-handle}

Your application should gracefully handle the following:

Type Example Response
Validation Error Missing name or invalid price { "error": "Invalid product data" } (400)
Not Found Error Product ID doesn't exist { "error": "Product not found" } (404)
File System Error products.json is missing/corrupted { "error": "Internal Server Error" } (500)
Undefined Route /random endpoint { "error": "Route not found" } (404)

Installing and Running Your Application

Run the following command to install all required dependencies:

npm install

Run the following command to start the server:

npm run dev

🧪 Testing Your Implementation {#testing-your-implementation}

A complete product.test.js file is provided inside /test.

Run tests with:

npm test

You should see test cases covering:

  • ✅ Successful CRUD operations (Create, Read, Update, Delete)

  • ❌ Error handling (Invalid data, Not Found, Corrupted File)

Keep running tests until all pass successfully!!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published