0
0
Expressframework~5 mins

Centralized error handler in Express - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a centralized error handler in Express?
It is a single middleware function that catches and processes all errors in an Express app, so you don't have to handle errors in every route separately.
Click to reveal answer
beginner
How do you define a centralized error handler middleware in Express?
You create a middleware function with four parameters: (err, req, res, next). Express recognizes it as an error handler and calls it when an error occurs.
Click to reveal answer
intermediate
Why should you place the centralized error handler after all other middleware and routes?
Because Express calls middleware in order, placing the error handler last ensures it catches errors from all previous middleware and routes.
Click to reveal answer
intermediate
What is the role of the 'next' function in an Express error handler?
It can be used to pass the error to the next error handler if you have multiple, or to continue the middleware chain if needed.
Click to reveal answer
beginner
How can you send a user-friendly error message from a centralized error handler?
You can set the HTTP status code and send a JSON or text response with a simple message explaining the error without exposing sensitive details.
Click to reveal answer
Which of these is the correct signature for an Express centralized error handler?
A(err, req, res, next)
B(req, res, next)
C(req, res)
D(err, req, res)
Where should you place the centralized error handler middleware in your Express app?
AIt doesn't matter
BBefore all routes
CIn the middle of routes
DAfter all routes and other middleware
What happens if you forget to call next() inside an error handler?
AThe request will hang and not continue
BNothing, it works fine
CExpress will crash
DThe error will be passed to the next middleware automatically
How can you send a 404 error using the centralized error handler?
AUse res.sendStatus(404) in routes only
BCall res.status(404).send() inside the error handler
CThrow an error with status 404 and pass it to next()
DYou cannot handle 404 errors in centralized error handler
What is a benefit of using a centralized error handler?
AIt makes the app slower
BIt reduces repeated error handling code in routes
CIt hides all errors from developers
DIt only works for syntax errors
Explain how to create and use a centralized error handler in an Express app.
Think about the special middleware signature and order.
You got /4 concepts.
    Describe why centralized error handling improves Express app code quality.
    Consider how handling errors in one place helps.
    You got /4 concepts.