Missing error handling middleware for asyncHandler
Author: Aanand-codeCreated Aug 25, 2025Updated Aug 25, 2025
Bug Report
The asyncHandler correctly catches errors and calls next(error), but there's no error handling middleware to actually send the response to the client.
✅ Solution Needed
Add an error handling middleware that:
- Catches errors passed to
next(error) - Sends properly formatted error responses
Code Suggestion
Create middleware/errorMiddleware.js:
const errorHandler = (err, req, res, next) => {
const statusCode = err.statusCode || 500
const message = err.message || "Internal Server Error"
res.status(statusCode).json({
success: false,
message: message,
...(process.env.NODE_ENV === 'development' && { stack: err.stack })
})
}
export { errorHandler }Source: hiteshchoudhary/chai-backend