0
0
Expressframework~10 mins

Error-handling middleware in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Error-handling middleware
Request received
Route handler runs
Error occurs?
NoSend response
Yes
Pass error to next()
Error-handling middleware runs
Send error response
End request
When a request causes an error, Express passes it to special middleware with 4 parameters to handle and respond with error info.
Execution Sample
Express
app.use((err, req, res, next) => {
  console.error(err.message);
  res.status(500).send('Server error');
});
This middleware catches errors, logs the message, and sends a 500 response.
Execution Table
StepActionError Present?Middleware CalledResponse Sent
1Request received at /dataNoRoute handlerNo
2Route handler runsYes (throws error)Route handlerNo
3Error passed to next()YesError-handling middlewareNo
4Error-handling middleware logs errorYesError-handling middlewareNo
5Error-handling middleware sends 500 responseYesError-handling middleware500 Server error
6Request endsNoNoneResponse sent
💡 Error handled and response sent, request cycle ends.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5
errundefinedError object with message 'Something broke!'Same error objectSame error object
res.statusCode200 (default)200 (default)200 (default)500
responseSentfalsefalsefalsetrue
Key Moments - 2 Insights
Why does error-handling middleware have 4 parameters instead of 3?
Express identifies error-handling middleware by the 4 parameters (err, req, res, next). This lets it run only when an error is passed, as shown in steps 3-5.
What happens if next() is called without an error in error-handling middleware?
If next() is called without an error, Express skips error middleware and looks for normal middleware, which can cause the error to be unhandled. See step 3 where next(err) is used to pass the error.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the error-handling middleware first run?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Check the 'Middleware Called' column to see when error-handling middleware starts.
According to the variable tracker, what is the response status code after step 5?
A500
B200
C404
Dundefined
💡 Hint
Look at the 'res.statusCode' row after step 5.
If the route handler did not throw an error, what would happen according to the flow?
AError-handling middleware runs anyway
Bnext(err) is called with undefined
CRequest ends after sending normal response
DServer crashes
💡 Hint
Refer to the concept flow where 'Error occurs?' is No.
Concept Snapshot
Error-handling middleware in Express:
- Has 4 parameters: (err, req, res, next)
- Runs only when next(err) is called
- Used to catch errors and send error responses
- Place after all other middleware
- Example: app.use((err, req, res, next) => { res.status(500).send('Error'); })
Full Transcript
In Express, error-handling middleware is special middleware with four parameters: err, req, res, and next. When a route handler or middleware calls next() with an error, Express skips normal middleware and runs error-handling middleware. This middleware can log the error and send a response like a 500 status code. The flow starts with a request, runs the route handler, and if an error occurs, passes it to error middleware which sends the error response. Variables like the error object and response status code change during this process. Understanding when error middleware runs and how it handles errors helps keep your app stable and user-friendly.