Express.js Masterclass
The Professional Backend Framework01.Home02.Introduction03.Express vs Native Node04.Express Generator05.Request Object (req)06.Response Object (res)07.Advanced Routing08.URL Params & Query09.Body Parsing10.Template Engines (EJS)11.Serving Static Files12.Middleware Architecture13.Must-Have Middleware14.File Uploads (Multer)15.Custom Error Handling16.Cookies & Sessions17.User Auth (Passport)18.Database Integration19.Data Validation20.Socket.io in Express21.Security & Helmet
Custom Error Handling
Error handling in Express is centralized using a special type of middleware that takes four arguments instead of three.
1. The Error Handling Middleware
You define error-handling middleware last, after other app.use() and routes calls.
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something broke!');
});2. Triggering Errors
Just pass the error to the next() function. Express will skip all remaining non-error handlers and go straight to the error middleware.
app.get('/user/:id', async (req, res, next) => {
try {
const user = await User.findById(req.params.id);
if (!user) {
const error = new Error('User not found');
error.status = 404;
return next(error);
}
res.json(user);
} catch (err) {
next(err); // Handles DB errors, etc.
}
});3. JSON Error Responses
In production, you want to send clean JSON errors instead of HTML stack traces.
app.use((err, req, res, next) => {
const statusCode = err.status || 500;
res.status(statusCode).json({
error: {
message: err.message,
status: statusCode
}
});
});Pro Pattern: Create an
AppError utility class that extends the standard Error class to include custom status codes and operational flags.