Challenge - 5 Problems
Express Error Response Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Express error handler?
Consider this Express error handling middleware. What JSON response will the client receive if an error with message 'Invalid input' and status 400 is passed to next()?
Express
app.use((err, req, res, next) => {
res.status(err.status || 500).json({
error: {
message: err.message || 'Internal Server Error',
status: err.status || 500
}
});
});Attempts:
2 left
💡 Hint
Look at how the error object properties are used in the JSON response.
✗ Incorrect
The middleware sets the response status to err.status or 500 and sends a JSON object with an error key containing message and status from the error object.
📝 Syntax
intermediate2:00remaining
Which option causes a syntax error in Express error middleware?
Identify which error handler code snippet will cause a syntax error.
Attempts:
2 left
💡 Hint
Check the brackets and parentheses carefully.
✗ Incorrect
Option B has a mismatched parenthesis in the JSON object causing a syntax error.
❓ state_output
advanced2:00remaining
What is the HTTP status code sent by this error handler?
Given this Express error middleware, what status code will be sent if err.status is undefined?
Express
app.use((err, req, res, next) => {
res.status(err.status || 404).json({
error: err.message || 'Not Found'
});
});Attempts:
2 left
💡 Hint
Look at the fallback value used with the OR operator.
✗ Incorrect
If err.status is undefined, the code uses 404 as the default status code.
🔧 Debug
advanced2:00remaining
Why does this error handler not send a JSON response?
This Express error handler does not send a JSON response as expected. What is the cause?
Express
app.use((err, req, res, next) => {
res.status(500);
res.json({ error: err.message });
next();
});Attempts:
2 left
💡 Hint
Think about what happens when next() is called after sending a response.
✗ Incorrect
Calling next() after res.json() passes control to next middleware, which can cause errors or multiple responses.
🧠 Conceptual
expert3:00remaining
Which option correctly formats an error response with a custom error code and message in Express?
You want to send a JSON error response with a custom code and message using Express error middleware. Which code snippet does this correctly?
Attempts:
2 left
💡 Hint
Check how status code and error details are included in the response.
✗ Incorrect
Option D sets status code with fallback and includes error code and message inside an error object in JSON.