Challenge - 5 Problems
Express Error Handler Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when an error is thrown in this Express route?
Consider this Express app snippet with a centralized error handler. What will the client receive if the route throws an error?
Express
const express = require('express'); const app = express(); app.get('/test', (req, res, next) => { throw new Error('Oops!'); }); app.use((err, req, res, next) => { res.status(500).json({ message: err.message }); });
Attempts:
2 left
💡 Hint
Think about how Express handles errors thrown inside routes and the role of the error middleware.
✗ Incorrect
When an error is thrown inside a route, Express passes it to the error-handling middleware. Here, the middleware sends a JSON response with the error message and status 500.
📝 Syntax
intermediate2:00remaining
Which option correctly defines an Express error-handling middleware?
Select the option that correctly defines a centralized error handler middleware in Express.
Attempts:
2 left
💡 Hint
Error middleware must have four parameters: err, req, res, next.
✗ Incorrect
Express recognizes error-handling middleware by the presence of four parameters. Option A correctly uses (err, req, res, next).
🔧 Debug
advanced2:00remaining
Why does this centralized error handler not catch errors thrown in async route handlers?
Given this code, errors thrown inside the async route are not caught by the error handler. Why?
Express
app.get('/async', async (req, res, next) => { throw new Error('Async error'); }); app.use((err, req, res, next) => { res.status(500).json({ error: err.message }); });
Attempts:
2 left
💡 Hint
Think about how errors in async functions behave in Express.
✗ Incorrect
Errors thrown inside async route handlers are not automatically caught by Express. They must be passed to next(err) or handled with try/catch to reach the error middleware.
❓ state_output
advanced2:00remaining
What is the response status and body when next() is called with an error?
In this Express app, what response does the client get when next(new Error('Fail')) is called inside a route?
Express
app.get('/fail', (req, res, next) => { next(new Error('Fail')); }); app.use((err, req, res, next) => { res.status(400).json({ message: err.message }); });
Attempts:
2 left
💡 Hint
Check how the error handler sets the status code and response body.
✗ Incorrect
The error handler sets status 400 and sends the error message as JSON, so the client receives status 400 with the message.
🧠 Conceptual
expert2:00remaining
Why should centralized error handlers be placed after all routes in Express?
Select the best explanation for why centralized error-handling middleware must be registered after all route handlers.
Attempts:
2 left
💡 Hint
Think about the flow of request handling in Express.
✗ Incorrect
Express processes middleware and routes in the order they are added. Error handlers must be last to catch errors from all previous middleware and routes.