0
0
Node.jsframework~10 mins

Error response formatting in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Error response formatting
Receive request
Process request
Error occurs?
NoSend success response
Yes
Format error response
Send error response
End
The server receives a request, processes it, checks for errors, formats an error response if needed, then sends it back.
Execution Sample
Node.js
app.get('/data', (req, res) => {
  try {
    throw new Error('Data not found');
  } catch (err) {
    res.status(404).json({ error: err.message });
  }
});
This code handles a GET request and sends a formatted JSON error response if an error occurs.
Execution Table
StepActionError Thrown?Response StatusResponse Body
1Request received at /dataNoN/AN/A
2Try block runsYes: 'Data not found'N/AN/A
3Catch block catches errorYes404N/A
4Format error responseYes404{"error":"Data not found"}
5Send error responseYes404{"error":"Data not found"}
💡 Error caught and formatted response sent with status 404
Variable Tracker
VariableStartAfter Step 2After Step 3Final
err.messageundefinedData not foundData not foundData not found
res.statusCode200 (default)200404404
response bodyemptyemptyempty{"error":"Data not found"}
Key Moments - 2 Insights
Why do we set the status code before sending the JSON response?
Because the status code tells the client what kind of response it is (error or success). The execution_table row 3 shows setting status 404 before sending the JSON body.
What happens if we don't catch the error?
The server might crash or send a generic error. The execution_table shows the catch block handling the error to send a clear message.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the response status code after step 3?
A404
B200
C500
DUndefined
💡 Hint
Check the 'Response Status' column at step 3 in the execution_table.
At which step is the error message added to the response body?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Response Body' column in the execution_table to see when the JSON error is formatted.
If the error message was 'User not found', how would the response body change at step 5?
A{"error":"Data not found"}
B{}
C{"error":"User not found"}
DNo response body
💡 Hint
Refer to variable_tracker for err.message and how it affects the response body.
Concept Snapshot
Error response formatting in Node.js:
- Catch errors in try-catch
- Set HTTP status code (e.g., 404)
- Send JSON with error message
- Helps clients understand what went wrong
- Always format errors before sending response
Full Transcript
When a Node.js server receives a request, it tries to process it. If an error happens, the catch block catches it. The server sets the HTTP status code to indicate an error, like 404 for not found. Then it sends a JSON response with an error message. This helps the client know what went wrong clearly. The execution table shows each step: receiving the request, error thrown, catching it, setting status, formatting JSON, and sending the response. Variables like err.message and res.statusCode change during these steps. Setting the status before sending the response is important so the client understands the error type. If errors are not caught, the server might crash or send unclear responses. Changing the error message updates the JSON sent back.