0
0
Expressframework~10 mins

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

Choose your learning style9 modes available
Concept Flow - Error-handling middleware signature
Request received
Middleware chain
Error occurs?
NoNext middleware
Yes
Error-handling middleware
Send error response
End request
This flow shows how Express passes requests through middleware and routes, and when an error occurs, it jumps to the error-handling middleware to send a response.
Execution Sample
Express
app.use((err, req, res, next) => {
  console.error(err.message);
  res.status(500).send('Something broke!');
});
This code defines an error-handling middleware that logs the error message and sends a 500 response.
Execution Table
StepMiddleware TypeArgumentsError Present?Action TakenResponse Sent
1Normal middleware(req, res, next)NoCall next middlewareNo
2Route handler(req, res, next)Yes (error passed)Skip to error middlewareNo
3Error-handling middleware(err, req, res, next)YesLog error and send 500 responseYes: 'Something broke!'
4End--Request ends after responseYes
💡 Error-handling middleware sends response, stopping further middleware.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
errundefinedundefinedError objectError objectError object
reqRequest objectRequest objectRequest objectRequest objectRequest object
resResponse objectResponse objectResponse objectResponse object with status 500Response sent
nextFunctionFunctionFunctionFunctionNot called after error middleware
Key Moments - 2 Insights
Why does error-handling middleware have four arguments instead of three?
Because Express identifies error-handling middleware by the presence of the first 'err' argument, as shown in the execution_table step 3 where the middleware signature is (err, req, res, next).
What happens if you forget to include the 'err' parameter in error middleware?
Express will treat it as normal middleware and not call it on errors, so errors won't be handled properly, as seen in step 2 where error skips normal middleware.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does Express detect an error and jump to error-handling middleware?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Check the 'Error Present?' and 'Action Taken' columns in execution_table rows.
According to variable_tracker, what is the value of 'err' after Step 1?
AError object
Bundefined
Cnull
DString message
💡 Hint
Look at the 'err' row under 'After Step 1' in variable_tracker.
If the error-handling middleware did not call res.status(500).send(), what would happen?
AThe request would hang without response
BExpress would automatically send a 404 response
CThe next middleware would handle the error
DThe error would be ignored and request continues
💡 Hint
Refer to execution_table step 3 and step 4 about response sending and request ending.
Concept Snapshot
Error-handling middleware in Express has 4 arguments: (err, req, res, next).
It catches errors passed from previous middleware or routes.
Use it to log errors and send error responses.
If missing 'err', Express won't treat it as error middleware.
Always end with sending a response or calling next(err).
Full Transcript
In Express, error-handling middleware is special because it has four parameters: err, req, res, and next. When an error happens in normal middleware or routes, Express skips normal middleware and jumps to the error-handling middleware. This middleware can log the error and send a response like status 500. If you forget the 'err' parameter, Express won't recognize it as error middleware, so errors won't be caught properly. The flow starts with request, goes through middleware, and if an error occurs, it jumps to error middleware which sends the response and ends the request.