Error-handling middleware helps catch and respond to errors in your app. It keeps your app from crashing and shows helpful messages.
Error-handling middleware signature in Express
Introduction
Syntax
Express
function errorHandler(err, req, res, next) {
// handle the error
}Error-handling middleware always has four parameters: err, req, res, and next.
Express knows it is error-handling middleware because of the four parameters.
Examples
Express
app.use(function (err, req, res, next) {
res.status(500).send('Something broke!')
})Express
app.use((err, req, res, next) => {
console.error(err.stack)
res.status(500).json({ error: err.message })
})Sample Program
This app throws an error on the home route. The error-handling middleware catches it, logs the message, and sends a 500 response.
Express
import express from 'express' const app = express() app.get('/', (req, res) => { throw new Error('Oops!') }) app.use((err, req, res, next) => { console.error('Error caught:', err.message) res.status(500).send('Internal Server Error') }) app.listen(3000, () => { console.log('Server running on http://localhost:3000') })
Important Notes
Always place error-handling middleware after all other middleware and routes.
Calling next() inside error middleware passes the error to the next error handler.
Summary
Error-handling middleware has four parameters: err, req, res, next.
It catches errors and prevents app crashes.
Use it to send user-friendly error messages and log problems.
Practice
1. Which of the following correctly describes the signature of an error-handling middleware function in Express?
easy
Solution
Step 1: Recall Express middleware function signatures
Normal middleware functions have three parameters:req, res, next. Error-handling middleware must have four parameters.Step 2: Identify the error-handling middleware signature
Error-handling middleware functions always start witherras the first parameter, followed byreq, res, next.Final Answer:
It has four parameters: err, req, res, next. -> Option DQuick Check:
Error middleware signature = err, req, res, next [OK]
Hint: Error middleware always has 4 parameters starting with err [OK]
Common Mistakes:
- Using only three parameters for error middleware
- Forgetting the err parameter at the start
- Adding extra parameters beyond four
2. Which of the following is the correct syntax to define an error-handling middleware in Express?
easy
Solution
Step 1: Check parameter order in error middleware
The correct order iserr, req, res, next. This order is required by Express to recognize error middleware.Step 2: Match the correct syntax
app.use(function(err, req, res, next) { /* code */ }); matches the correct order and syntax for error-handling middleware.Final Answer:
app.use(function(err, req, res, next) { /* code */ }); -> Option BQuick Check:
Correct parameter order = err, req, res, next [OK]
Hint: Error middleware params start with err, then req, res, next [OK]
Common Mistakes:
- Placing err parameter last or in the wrong position
- Swapping req and res parameters
- Using incorrect function parameter count
3. What will happen when this Express error-handling middleware is added?
app.use(function(err, req, res, next) {
res.status(500).send('Error: ' + err.message);
});medium
Solution
Step 1: Understand error middleware behavior
Error-handling middleware catches errors passed by previous middleware or routes.Step 2: Analyze the middleware code
This middleware sends a 500 status and the error message as a response, handling the error gracefully.Final Answer:
It catches errors and sends a 500 status with the error message. -> Option AQuick Check:
Error middleware sends 500 and message = It catches errors and sends a 500 status with the error message. [OK]
Hint: Error middleware sends response with status 500 and error message [OK]
Common Mistakes:
- Thinking next() must always be called in error middleware
- Assuming error middleware crashes app without next()
- Confusing error middleware with normal middleware
4. Identify the error in this Express error-handling middleware:
app.use(function(req, res, next, err) {
console.error(err);
res.status(500).send('Server error');
});medium
Solution
Step 1: Check parameter order for error middleware
Error-handling middleware must have parameters in order:err, req, res, next. Here,erris last, which is incorrect.Step 2: Understand impact of wrong parameter order
Express will not recognize this as error middleware, so errors won't be caught properly.Final Answer:
The parameters are in the wrong order; err must be first. -> Option CQuick Check:
err parameter must be first in error middleware [OK]
Hint: Error middleware must start with err parameter [OK]
Common Mistakes:
- Placing err parameter anywhere but first
- Assuming next() call is mandatory in all error middleware
- Confusing error middleware with normal middleware signature
5. You want to create an error-handling middleware that logs the error and sends a JSON response with status 500. Which of the following is the best correct implementation?
hard
Solution
Step 1: Verify parameter count and order
Error middleware must have exactly four parameters in order:err, req, res, next. app.use((err, req, res, next) => { console.log(err); res.status(500).json({error: err.message}); }); matches this.Step 2: Check response status and format
app.use((err, req, res, next) => { console.log(err); res.status(500).json({error: err.message}); }); sets status 500 and sends JSON with error message, which is correct and user-friendly.Final Answer:
app.use((err, req, res, next) => { console.log(err); res.status(500).json({error: err.message}); }); -> Option AQuick Check:
Correct signature + status 500 + JSON response = app.use((err, req, res, next) => { console.log(err); res.status(500).json({error: err.message}); }); [OK]
Hint: Error middleware needs 4 params and sets status before sending JSON [OK]
Common Mistakes:
- Wrong parameter order or count
- Forgetting to set status code
- Sending response without JSON format
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
