0
0
Expressframework~10 mins

Synchronous error handling in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Synchronous error handling
Request received
Route handler starts
Code runs synchronously
Error occurs?
NoSend response
Yes
Throw error
Express catches error
Error-handling middleware runs
Send error response
This flow shows how Express handles errors thrown synchronously inside route handlers by passing them to error middleware.
Execution Sample
Express
app.get('/test', (req, res) => {
  if (!req.query.name) {
    throw new Error('Name required');
  }
  res.send(`Hello ${req.query.name}`);
});
This code throws an error synchronously if the 'name' query is missing, which Express catches and forwards to error middleware.
Execution Table
StepActionConditionResultNext
1Request to /test without name queryN/ARoute handler startsCheck if name exists
2Check if req.query.name existsreq.query.name is undefinedCondition true (missing)Throw error
3Throw new Error('Name required')Error thrownExpress catches errorPass to error middleware
4Error-handling middleware runsReceives errorSends error response to clientEnd
5Request to /test with name queryN/ARoute handler startsCheck if name exists
6Check if req.query.name existsreq.query.name is presentCondition falseSend greeting response
7Send res.send(`Hello ${req.query.name}`)Response sentClient receives greetingEnd
💡 Execution stops after sending response or error response to client.
Variable Tracker
VariableStartAfter Step 2After Step 6Final
req.query.nameundefined or stringundefined (error path) or string (success path)string (success path)unchanged
errornoneError('Name required') thrownnonenone
Key Moments - 2 Insights
Why does throwing an error inside the route handler not crash the server?
Express automatically catches synchronous errors thrown in route handlers and passes them to error-handling middleware, as shown in execution_table steps 3 and 4.
What happens if the error-handling middleware is missing?
Without error middleware, Express will send a default error response, but the server stays running. The error is still caught internally, preventing a crash.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 3 when the error is thrown?
AThe server crashes immediately
BThe request is ignored
CExpress catches the error and passes it to error middleware
DThe response is sent successfully
💡 Hint
Check the 'Result' column in step 3 of the execution_table
At which step does the route handler send a successful greeting response?
AStep 7
BStep 6
CStep 4
DStep 2
💡 Hint
Look for 'Send res.send' action in the execution_table
If req.query.name is missing, which step shows the error being handled?
AStep 2
BStep 4
CStep 1
DStep 7
💡 Hint
Find where error-handling middleware runs in the execution_table
Concept Snapshot
Synchronous error handling in Express:
- Throw errors inside route handlers synchronously.
- Express catches these errors automatically.
- Errors go to error-handling middleware.
- Middleware sends error response.
- Server stays running safely.
Full Transcript
In Express, when a route handler runs, it executes code synchronously. If an error is thrown inside this code, Express catches it automatically. This prevents the server from crashing. The error is passed to special error-handling middleware, which sends an error response to the client. If no error occurs, the handler sends a normal response. This flow ensures safe and clear error handling for synchronous code.