0
0
Node.jsframework~10 mins

Centralized error handling in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Centralized error handling
Start Request
Execute Middleware/Route
Error Occurs?
NoSend Response
Yes
Pass Error to next()
Centralized Error Handler
Send Error Response
End Request
This flow shows how errors in Node.js apps are caught and passed to a single error handler that sends the response.
Execution Sample
Node.js
app.get('/data', (req, res, next) => {
  if (!req.query.id) {
    next(new Error('ID missing'));
  } else {
    res.send('Data for ' + req.query.id);
  }
});
This code checks if 'id' is in query; if missing, it passes an error to the centralized handler.
Execution Table
StepActionConditionResultNext Step
1Request to /data with no idreq.query.id undefinedError created: 'ID missing'Call next(error)
2next(error) calledError passedSkip normal handlersCentralized error handler called
3Centralized error handler runsReceives errorSends 500 response with error messageRequest ends
4Request to /data with id=5req.query.id = '5'No errorSend response 'Data for 5'
5Response sentN/ARequest ends normallyEnd
💡 Execution stops after sending response or error response to client.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
req.query.idundefinedundefinedundefinedundefinedundefined
errornoneError('ID missing')passed to next()received in handlerhandled
response statusnonenonenone500sent
response bodynonenonenone'ID missing'sent
Key Moments - 2 Insights
Why do we call next(error) instead of sending the response directly?
Calling next(error) passes the error to the centralized handler, keeping error logic in one place (see execution_table step 2). Sending response directly would duplicate error handling.
What happens if no error occurs in the route?
The route sends the normal response and the centralized error handler is not called (see execution_table step 4 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the response status code sent when an error occurs?
A404
B500
C200
D302
💡 Hint
Check the 'response status' variable in variable_tracker after step 3.
At which step does the centralized error handler get called?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Next Step' column in execution_table after step 2.
If req.query.id is present, what happens according to the execution table?
AError is created and passed
BCentralized error handler runs
CResponse with data is sent
DRequest ends with error
💡 Hint
See steps 4 and 5 in execution_table for normal flow.
Concept Snapshot
Centralized error handling in Node.js:
- Use next(error) to pass errors
- Define one error handler middleware
- Error handler sends error response
- Keeps error logic in one place
- Normal routes send responses if no error
Full Transcript
Centralized error handling in Node.js means when an error happens in a route or middleware, you call next(error) instead of sending a response directly. This passes the error to a special error handler middleware defined once in your app. This handler receives the error and sends a proper error response to the client, like a 500 status with a message. If no error occurs, the route sends the normal response and the error handler is not called. This approach keeps your error handling code in one place, making your app easier to maintain and debug.