What is the purpose of error response formatting in Node.js APIs?
Error response formatting helps send clear, consistent messages to clients when something goes wrong. It makes debugging easier and improves user experience by providing understandable error details.
Click to reveal answer
beginner
Which HTTP status code is commonly used for a generic server error?
The HTTP status code 500 is used for generic server errors, indicating that something went wrong on the server side.
Click to reveal answer
intermediate
Why should error responses include a consistent JSON structure?
Consistent JSON structure allows clients to easily parse and handle errors programmatically. It also helps frontend developers display meaningful messages to users.
Click to reveal answer
beginner
Show a simple example of a JSON error response format.
A simple JSON error response might look like: {"error": {"message": "Not Found", "code": 404}}. This clearly tells the client what went wrong and the error code.
Click to reveal answer
intermediate
What is a good practice when sending error details in production environments?
In production, avoid sending sensitive error details to clients. Instead, send a generic message and log the detailed error on the server for developers to review.
Click to reveal answer
Which HTTP status code indicates a client error due to bad request?
A400
B500
C200
D302
✗ Incorrect
400 means Bad Request, indicating the client sent invalid data.
What is the main benefit of formatting error responses as JSON?
AIt speeds up the server
BIt makes errors easier to read and handle by clients
CIt hides all error details
DIt changes the HTTP status code automatically
✗ Incorrect
JSON formatting helps clients parse and display errors clearly.
Which of these is NOT a good practice for error response formatting?
ALogging detailed errors on the server
BSending consistent error structure
CUsing proper HTTP status codes
DIncluding sensitive server details in production errors
✗ Incorrect
Avoid exposing sensitive details to clients in production.
What should an error response include besides the HTTP status code?
AA clear error message
BThe full server stack trace
CClient's IP address
DUser's password
✗ Incorrect
A clear message helps clients understand the error.
In Node.js, which method is commonly used to send a JSON error response?
Aconsole.log()
Bres.sendFile()
Cres.json()
Dprocess.exit()
✗ Incorrect
res.json() sends a JSON response to the client.
Explain why consistent error response formatting is important in Node.js APIs.
Think about how clients and developers benefit from predictable error messages.
You got /4 concepts.
Describe best practices for sending error responses in production environments.
Consider security and user experience.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using res.status() in Node.js error handling?
easy
A. To log errors on the server console
B. To send a success message to the client
C. To restart the server automatically
D. To set the HTTP status code for the response
Solution
Step 1: Understand HTTP status codes
HTTP status codes tell the client if a request succeeded or failed.
Step 2: Role of res.status()
This method sets the HTTP status code in the response, indicating success or error.
Final Answer:
To set the HTTP status code for the response -> Option D
Quick Check:
res.status() sets HTTP status code [OK]
Hint: Remember: status sets code, not message or logs [OK]
Common Mistakes:
Confusing status with sending messages
Thinking it logs errors
Assuming it restarts the server
2. Which of the following is the correct way to send a JSON error response with status 404 in Express?
easy
A. res.send(404).json({ error: 'Not found' })
B. res.status(404).json({ error: 'Not found' })
C. res.json(404, { error: 'Not found' })
D. res.error(404).send({ error: 'Not found' })
Solution
Step 1: Check Express response methods
Use res.status(code) to set status, then json() to send JSON.
Step 2: Validate syntax correctness
Only res.status(404).json({ error: 'Not found' }) is valid syntax.
Final Answer:
res.status(404).json({ error: 'Not found' }) -> Option B
Quick Check:
Set status then send JSON [OK]
Hint: Use res.status(code).json(object) for error responses [OK]
A. Missing the next parameter in the middleware function
B. Using res.status() instead of res.sendStatus()
C. Not calling res.end() after json()
D. Incorrect status code for errors
Solution
Step 1: Check Express error middleware signature
Error middleware must have four parameters: err, req, res, next.
Step 2: Identify missing parameter
The code has only three parameters, missing next, so Express won't recognize it as error handler.
Final Answer:
Missing the next parameter in the middleware function -> Option A
Quick Check:
Error middleware needs 4 params: err, req, res, next [OK]
Hint: Error middleware must have 4 parameters [OK]
Common Mistakes:
Leaving out next parameter
Confusing res.status() with res.sendStatus()
Thinking json() needs res.end()
5. You want to send a detailed error response with a custom code and message, but only if the error has a statusCode property. Otherwise, send a 500 status with a generic message. Which code snippet correctly implements this in Express?