0
0
Expressframework~20 mins

Error-handling middleware signature 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!
📝 Syntax
intermediate
2:00remaining
Identify the correct error-handling middleware signature in Express
Which of the following function signatures correctly defines an error-handling middleware in Express?
A(err, req, res, next) => { /* handle error */ }
B(req, res, next) => { /* handle error */ }
C(err, req, res) => { /* handle error */ }
D(req, res) => { /* handle error */ }
Attempts:
2 left
💡 Hint
Error-handling middleware in Express always has four parameters, starting with the error object.
component_behavior
intermediate
2:00remaining
What happens when an error is passed to next() in Express?
In Express, if you call next(err) inside a middleware, what will Express do next?
ARestart the middleware stack from the beginning
BContinue to the next regular middleware ignoring the error
CSkip all remaining non-error middleware and call the next error-handling middleware
DStop processing and send a default error response immediately
Attempts:
2 left
💡 Hint
Think about how Express knows to handle errors differently.
🔧 Debug
advanced
2:00remaining
Find the error in this Express error-handling middleware
What error will this middleware cause when an error occurs? function errorHandler(req, res, next) { res.status(500).send('Something broke!'); }
Express
function errorHandler(req, res, next) {
  res.status(500).send('Something broke!');
}
AIt will not be recognized as error-handling middleware because it lacks the 'err' parameter
BIt will cause a runtime error because 'next' is unused
CIt will send a 404 status instead of 500
DIt will cause a syntax error due to missing parentheses
Attempts:
2 left
💡 Hint
Error-handling middleware must have four parameters, starting with the error object.
state_output
advanced
2:00remaining
What is the response status code sent by this error-handling middleware?
Given this Express error-handling middleware, what status code will the client receive? function errorHandler(err, req, res, next) { if (err.status) { res.status(err.status).send(err.message); } else { res.status(500).send('Internal Server Error'); } }
Express
function errorHandler(err, req, res, next) {
  if (err.status) {
    res.status(err.status).send(err.message);
  } else {
    res.status(500).send('Internal Server Error');
  }
}
AThrows an error because res.status is called twice
BAlways 200 regardless of error
CAlways 404 regardless of error
D500 if err.status is undefined, otherwise err.status
Attempts:
2 left
💡 Hint
Check how the status code is set depending on the error object.
🧠 Conceptual
expert
3:00remaining
Why must error-handling middleware have four parameters in Express?
Why does Express require error-handling middleware to have exactly four parameters: (err, req, res, next)?
ABecause JavaScript requires four parameters for error handling functions
BBecause Express uses the function's parameter count to distinguish error handlers from regular middleware
CBecause the 'next' parameter is mandatory for all middleware, including error handlers
DBecause the 'err' parameter is optional and only used in some error handlers
Attempts:
2 left
💡 Hint
Think about how Express decides which middleware to call on errors.