The Right Way to Handle Errors in Express.js REST APIs

Thapa Technical
Thapa Technical
63 followers
Nov 26, 2026

How does Express 5 error handling middleware turn a thrown error into a JSON response? In this animated Node.js REST API tutorial, we’ll build a custom error class called AppError and follow one failed request from start to finish.

Imagine you’re adding a course, but another course already uses the same slug. Your API should return 409 Conflict. So how does the 409 you pass to new AppError() reach the client?

Using the Courses API from our live backend class, we’ll walk through:

• How AppError extends JavaScript’s Error class
• What super(message) does and why we store statusCode on the error
• How ensureUniqueSlug() detects a duplicate and throws AppError
• How Express error middleware uses (err, req, res, next)
• Why err.statusCode ?? 500 gives unexpected errors a fallback
• How to log a 500 error on the server and send a simple message to the client

Quick challenge 👇
If you replace AppError with a plain new Error("A course with this title already exists"), what status code will this middleware send back, and what message will the client see? Drop both answers in the comments.

------------------------------------------------------------------------------------
🔗 Useful Playlists

📘 Next.js Full Stack Playlist:
https://youtube.com/playlist?list=PLwGdqUZWnOp0lwvSBaIzzgV9X0ZiZ-42O&si=aQ_TNBNNx5L7V_bn

⚛️ React.js Playlist:
https://youtube.com/playlist?list=PLwGdqUZWnOp1Rab71vx2zMF6qpwGDB2Z1&si=sDZRxsYNetYmojKd

💻 TypeScript Playlist:
https://youtube.com/playlist?list=PLwGdqUZWnOp0xfHQFmlL52b_6-QZ0mnk_&si=c4jfsd5ElDC1p0A5
------------------------------------------------------------------------------------
💸 Get All My YouTube Videos' Source Code for just ₹199! Grab Now - https://thapatechnical.shop/source-code

📺 Watch the complete Node.JS Playlist here :https://youtube.com/playlist?list=PLwGdqUZWnOp3KELplHtc-RnJ5xTUPqdgH&si=_ob8F88HYxCdspup

🔥 Access Source Code, PPT & Notes here for Free : https://www.thapatechnical.com/2024/11/introduction-to-nodejs.html
------------------------------------------------------------------------

🚀 Boost Your Skills with these Pre-Requisite Videos:

🔗 Best HTML Course - https://youtu.be/5ccq_nLHneE
🔗 Best CSS Course - https://youtu.be/MSICFljRcb4
🔗 JavaScript Basics Course Part 1 - https://youtu.be/13gLB6hDHR8
🔗 JavaScript Advanced Course Part 2 - https://youtu.be/YwsOCN8woA8

------------------------------------------------------------------------
💸 Build Your Own E-commerce Website for Free! - https://www.thapatechnical.com/2024/03/build-complete-ecommerce-website-using.html

------------------------------------------------------------------------
✌️ Join Us!

🚀 Become a Member: Unlock perks, free source code, and more Join Now : https://www.youtube.com/channel/UCwfaAHy4zQUb2APNOGXUCCA/join
📷 Connect on Instagram: https://www.instagram.com/thapatechnical
🗨️ Join Our Discord Server: Hang out with fellow programmers Discord Link: https://discord.gg/MdScmCsua6

------------------------------------------------------------------------

⌛TIMELINE⏳