0
0
Expressframework~10 mins

Error response formatting in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Error response formatting
Request received
Process request
Error occurs?
NoSend success response
Yes
Format error response
Send error response
End request
This flow shows how Express handles a request, detects an error, formats an error response, and sends it back.
Execution Sample
Express
app.get('/user', (req, res) => {
  try {
    throw new Error('User not found');
  } catch (err) {
    res.status(404).json({ error: err.message });
  }
});
This code handles a GET request, throws an error, catches it, and sends a JSON error response with status 404.
Execution Table
StepActionError Thrown?Response StatusResponse Body
1Request to /user receivedNoN/AN/A
2Try block executesYes: 'User not found'N/AN/A
3Catch block catches errorYesSet status 404Prepare JSON { error: 'User not found' }
4Send responseYes404{ "error": "User not found" }
5Request endsN/AN/AN/A
💡 Error caught and formatted response sent with status 404
Variable Tracker
VariableStartAfter Step 2After Step 3Final
err.messageundefinedUser not foundUser not foundUser not found
res.statusCode200 (default)200404404
res.bodyemptyempty{ error: 'User not found' }{ error: 'User not found' }
Key Moments - 2 Insights
Why do we use res.status(404) before sending the JSON response?
Because res.status(404) sets the HTTP status code to 404, indicating 'Not Found'. The execution_table step 3 shows setting status before sending the JSON error message.
What happens if we forget to catch the error?
The server might crash or send a generic error. The execution_table shows catching the error at step 3 to format a proper response.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the response status at step 4?
A404
B200
C500
DUndefined
💡 Hint
Check the 'Response Status' column at step 4 in the execution_table.
At which step is the error message 'User not found' assigned to err.message?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Error Thrown?' column in the execution_table.
If we remove res.status(404), what will be the default response status?
A404
B500
C200
D400
💡 Hint
Check the 'res.statusCode' variable in variable_tracker before step 3.
Concept Snapshot
Express error response formatting:
- Use try-catch to handle errors
- In catch, set status with res.status(code)
- Send JSON error with res.json({ error: message })
- Status code informs client about error type
- Always catch errors to avoid crashes
Full Transcript
When Express receives a request, it processes it inside a try block. If an error occurs, it is caught in the catch block. There, the response status is set to an appropriate error code like 404. Then, a JSON object with the error message is sent back to the client. This ensures the client knows what went wrong. Without catching errors, the server might crash or send unclear responses. Setting the status before sending the JSON is important so the client receives the correct HTTP status code.