0
0
Expressframework~20 mins

Centralized error handler in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Error Handler Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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 });
});
AEmpty response with status 200
BServer crashes with unhandled exception
C{"error":"Internal Server Error"} with status 500
D{"message":"Oops!"} with status 500
Attempts:
2 left
💡 Hint
Think about how Express handles errors thrown inside routes and the role of the error middleware.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines an Express error-handling middleware?
Select the option that correctly defines a centralized error handler middleware in Express.
Aapp.use((err, req, res, next) => { res.status(500).send('Error'); });
Bapp.use((req, res, next) => { res.status(500).send('Error'); });
Capp.use((err, req, res) => { res.status(500).send('Error'); });
Dapp.use((error, request, response) => { response.status(500).send('Error'); });
Attempts:
2 left
💡 Hint
Error middleware must have four parameters: err, req, res, next.
🔧 Debug
advanced
2: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 });
});
ABecause the error handler is defined after the route, so it is ignored.
BBecause Express does not support async functions in routes.
CBecause errors in async functions must be passed to next(err) explicitly or caught with try/catch.
DBecause the error handler middleware is missing the 'next' parameter.
Attempts:
2 left
💡 Hint
Think about how errors in async functions behave in Express.
state_output
advanced
2: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 });
});
AStatus 500 with JSON {"message":"Fail"}
BStatus 400 with JSON {"message":"Fail"}
CStatus 200 with empty body
DNo response, request hangs
Attempts:
2 left
💡 Hint
Check how the error handler sets the status code and response body.
🧠 Conceptual
expert
2: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.
ABecause Express executes middleware in order, so error handlers must come last to catch errors from previous routes.
BBecause error handlers only work if placed before routes to intercept errors early.
CBecause placing error handlers before routes causes syntax errors.
DBecause error handlers automatically run in parallel with routes regardless of order.
Attempts:
2 left
💡 Hint
Think about the flow of request handling in Express.