Challenge - 5 Problems
Express Error Handling 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 middleware error handling?
Consider this Express middleware function. What will the server respond with when a synchronous error occurs?
Express
const express = require('express'); const app = express(); app.use((req, res, next) => { throw new Error('Synchronous error'); }); app.use((err, req, res, next) => { res.status(500).send('Error caught: ' + err.message); }); app.listen(3000);
Attempts:
2 left
💡 Hint
Think about how Express handles errors thrown inside middleware.
✗ Incorrect
Express catches synchronous errors thrown in middleware and passes them to error-handling middleware, which sends the response with status 500 and the error message.
❓ Predict Output
intermediate2:00remaining
What happens if next() is called with an error synchronously?
Look at this Express route handler. What will the server respond with when the route is accessed?
Express
const express = require('express'); const app = express(); app.get('/', (req, res, next) => { next(new Error('Error passed to next')); }); app.use((err, req, res, next) => { res.status(500).send('Caught error: ' + err.message); }); app.listen(3000);
Attempts:
2 left
💡 Hint
What does calling next() with an error do in Express?
✗ Incorrect
Calling next() with an error passes control to the error-handling middleware, which sends the 500 response with the error message.
❓ component_behavior
advanced2:30remaining
Which middleware order causes synchronous errors to be caught correctly?
Given these middleware registrations, which order ensures synchronous errors thrown in routes are caught by the error handler?
Express
const express = require('express'); const app = express(); function errorHandler(err, req, res, next) { res.status(500).send('Error: ' + err.message); } function routeHandler(req, res) { throw new Error('Oops'); } // Options show different orders of app.use and app.get calls
Attempts:
2 left
💡 Hint
Error-handling middleware must be registered after routes to catch their errors.
✗ Incorrect
Express calls middleware in order. The error handler must come after routes to catch errors thrown by them.
📝 Syntax
advanced2:00remaining
Which error-handling middleware signature is correct for synchronous errors?
Which of these middleware functions is correctly defined to catch synchronous errors in Express?
Attempts:
2 left
💡 Hint
Error-handling middleware must have four parameters.
✗ Incorrect
Express recognizes error handlers by their four parameters: err, req, res, next.
🔧 Debug
expert3:00remaining
Why does this synchronous error crash the Express server?
This code throws a synchronous error inside a route, but the server crashes instead of sending a response. Why?
Express
const express = require('express'); const app = express(); app.get('/', (req, res) => { throw new Error('Crash!'); }); app.listen(3000);
Attempts:
2 left
💡 Hint
Express needs special middleware to catch errors and prevent crashes.
✗ Incorrect
Without error-handling middleware, synchronous errors thrown in routes crash the server.