Challenge - 5 Problems
Error Response Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Express error handler?
Consider this Express.js error handling middleware. What JSON response will the client receive if an error with message 'Invalid input' and status 400 is passed?
Node.js
app.use((err, req, res, next) => {
res.status(err.status || 500).json({
error: {
message: err.message || 'Internal Server Error',
code: err.status || 500
}
});
});Attempts:
2 left
💡 Hint
Look at how the error message and status are accessed and structured in the JSON response.
✗ Incorrect
The middleware sends a JSON object with an 'error' key containing 'message' and 'code' properties. It uses the error's message and status if provided, otherwise defaults.
❓ Predict Output
intermediate2:00remaining
What error does this Express error handler cause?
What error will this Express error handler cause when an error is passed to it?
Node.js
app.use((err, req, res, next) => {
res.status(err.status).json({
error: err.message
});
});Attempts:
2 left
💡 Hint
Consider what happens if err.status is undefined.
✗ Incorrect
If err.status is undefined, res.status(undefined) defaults to 200 OK in Express, so no error is thrown. The JSON contains the error message but status code is 200.
❓ component_behavior
advanced2:00remaining
Which option correctly formats a JSON error response with stack trace only in development?
You want to send an error response JSON that includes the error message always, but includes the stack trace only if NODE_ENV is 'development'. Which code snippet does this correctly?
Attempts:
2 left
💡 Hint
Check the environment variable condition for including the stack trace.
✗ Incorrect
Option C includes the stack trace only if NODE_ENV is 'development', which is the common practice to avoid exposing stack traces in production.
📝 Syntax
advanced2:00remaining
Which option causes a syntax error in this error response code?
Identify the option that will cause a syntax error when used as an Express error handler response.
Node.js
app.use((err, req, res, next) => {
res.status(500).json({
error: err.message,
details: err.stack
});
});Attempts:
2 left
💡 Hint
Look for missing commas or invalid object syntax.
✗ Incorrect
Option B misses a comma between properties, causing a syntax error.
🔧 Debug
expert3:00remaining
Why does this Express error handler send a 200 status instead of 500?
Given this error handler, why does the client receive a 200 OK status instead of 500 when an error occurs?
app.use((err, req, res, next) => {
res.status(err.status).json({ error: err.message });
});
Attempts:
2 left
💡 Hint
Check what happens if err.status is not set.
✗ Incorrect
If err.status is undefined, Express treats res.status(undefined) as res.status(200), so the response status is 200 OK instead of an error code.