0
0
Expressframework~20 mins

Error-handling middleware in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does error-handling middleware behave in Express?
Consider an Express app with a route that throws an error. What happens when an error-handling middleware is defined with four parameters?

Which statement best describes the behavior?
AThe error-handling middleware runs for every request, regardless of errors.
BThe error-handling middleware only handles errors if they are thrown asynchronously inside setTimeout.
CThe error-handling middleware catches errors passed with next(err) or thrown synchronously and handles them.
DThe error-handling middleware must be the first middleware to catch errors.
Attempts:
2 left
💡 Hint
Error-handling middleware in Express has a special signature with four parameters.
📝 Syntax
intermediate
2:00remaining
Identify the correct error-handling middleware syntax in Express
Which of the following middleware functions is correctly defined as an error-handling middleware in Express?
Afunction (req, res) { res.status(500).send('Error occurred'); }
Bfunction (err, req, res, next) { res.status(500).send('Error occurred'); }
Cfunction (req, res, next) { res.status(500).send('Error occurred'); }
Dfunction (err, req, res) { res.status(500).send('Error occurred'); }
Attempts:
2 left
💡 Hint
Error-handling middleware must have four parameters.
🔧 Debug
advanced
2:00remaining
Why does this error-handling middleware not catch errors?
Given this Express app code:
app.get('/test', (req, res) => { throw new Error('fail'); });
app.use((req, res, next) => { res.status(404).send('Not found'); });
app.use((err, req, res, next) => { res.status(500).send('Server error'); });

Why does the error-handling middleware not catch the thrown error?
ABecause the 404 middleware is placed before the error-handling middleware and does not call next(err).
BBecause the error-handling middleware must be defined before any routes.
CBecause the thrown error is asynchronous and not caught by Express.
DBecause the error-handling middleware is missing the next parameter.
Attempts:
2 left
💡 Hint
Middleware order matters in Express.
state_output
advanced
2:00remaining
What is the response status code when an error is passed to next()?
In this Express app snippet:
app.get('/fail', (req, res, next) => { next(new Error('fail')); });
app.use((err, req, res, next) => { res.status(400).send('Bad request'); });

What status code will the client receive when requesting /fail?
A404
B500
C200
D400
Attempts:
2 left
💡 Hint
The error-handling middleware sets the status code explicitly.
🧠 Conceptual
expert
2:00remaining
Which statement about error-handling middleware chaining is true?
In Express, if multiple error-handling middleware are defined, how does Express decide which one to run?
AExpress runs error-handling middleware in the order they are defined until one sends a response or calls next().
BExpress runs only the first error-handling middleware and ignores the rest.
CExpress runs all error-handling middleware in parallel.
DExpress randomly picks one error-handling middleware to run.
Attempts:
2 left
💡 Hint
Think about how middleware chaining works in Express.