Bird
Raised Fist0
Expressframework~20 mins

Centralized error handler in Express - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a centralized error handler in an Express app?
easy
A. To serve static files efficiently
B. To speed up the server response time
C. To catch and handle all errors in one place
D. To manage user sessions

Solution

  1. Step 1: Understand error handling in Express

    Express allows middleware to catch errors and handle them centrally.
  2. Step 2: Identify the role of centralized error handler

    It collects all errors from routes and middleware to respond consistently.
  3. Final Answer:

    To catch and handle all errors in one place -> Option C
  4. Quick Check:

    Centralized error handler = catch all errors [OK]
Hint: Centralized error handler catches errors globally, not locally [OK]
Common Mistakes:
  • Thinking it improves server speed
  • Confusing with static file serving
  • Mixing with session management
2. Which of the following is the correct way to define a centralized error handler middleware in Express?
easy
A. app.use((req, res) => { res.status(500).send('Error'); });
B. app.use((err, req, res, next) => { res.status(500).send(err.message); });
C. app.get('/error', (err, req, res) => { res.send('Error'); });
D. app.use((req, res, next) => { res.send('Error'); });

Solution

  1. Step 1: Recall Express error handler signature

    Error handlers have four parameters: (err, req, res, next).
  2. Step 2: Match the correct middleware signature

    app.use((err, req, res, next) => { res.status(500).send(err.message); }); uses all four parameters correctly and sends error message.
  3. Final Answer:

    app.use((err, req, res, next) => { res.status(500).send(err.message); }); -> Option B
  4. Quick Check:

    Error handler middleware needs 4 params [OK]
Hint: Error handler middleware always has 4 parameters (err, req, res, next) [OK]
Common Mistakes:
  • Using only 3 parameters in error handler
  • Defining error handler as route handler
  • Missing the 'err' parameter
3. Given this Express code snippet, what will be the response if an error occurs in the route?
app.get('/test', (req, res, next) => {
  next(new Error('Oops!'));
});

app.use((err, req, res, next) => {
  res.status(500).send('Error caught: ' + err.message);
});
medium
A. Status 500 with body 'Error caught: Oops!'
B. Status 200 with body 'Oops!'
C. Status 404 with body 'Not Found'
D. No response, server crashes

Solution

  1. Step 1: Understand error propagation in route

    The route calls next() with an Error object, triggering error middleware.
  2. Step 2: Check error handler response

    The error handler sends status 500 and message including error text.
  3. Final Answer:

    Status 500 with body 'Error caught: Oops!' -> Option A
  4. Quick Check:

    Error passed to handler = 500 response with message [OK]
Hint: next(error) triggers centralized error handler with status 500 [OK]
Common Mistakes:
  • Expecting status 200 instead of 500
  • Thinking error handler is skipped
  • Assuming server crashes on error
4. Identify the error in this Express app setup for centralized error handling:
app.use((req, res, next) => {
  throw new Error('Fail');
});

app.use((req, res, next) => {
  res.status(500).send('Error occurred');
});
medium
A. Error handler middleware missing 'err' parameter
B. Throwing error inside middleware is not allowed
C. Error handler middleware should be placed before routes
D. Middleware order is correct, no error

Solution

  1. Step 1: Check error handler signature

    Error handlers must have 4 parameters: (err, req, res, next).
  2. Step 2: Analyze given error handler

    The second middleware has only (req, res, next), so it won't catch errors.
  3. Final Answer:

    Error handler middleware missing 'err' parameter -> Option A
  4. Quick Check:

    Error handler needs 4 params to catch errors [OK]
Hint: Error handler must have 4 parameters to work [OK]
Common Mistakes:
  • Forgetting the 'err' parameter in error handler
  • Placing error handler before routes
  • Thinking throwing error is disallowed
5. You want to create a centralized error handler that logs errors and sends a JSON response with status code and message. Which code snippet correctly implements this?
hard
A. app.use((req, res, next) => { console.error('Error'); res.json({ error: 'Unknown' }); });
B. app.use((err, req, res, next) => { res.send('Error occurred'); });
C. app.use((err, req, res) => { res.status(500).send('Error'); });
D. app.use((err, req, res, next) => { console.error(err); res.status(err.status || 500).json({ error: err.message }); });

Solution

  1. Step 1: Confirm error handler signature and logging

    app.use((err, req, res, next) => { console.error(err); res.status(err.status || 500).json({ error: err.message }); }); uses (err, req, res, next) and logs error with console.error.
  2. Step 2: Check response format and status code

    app.use((err, req, res, next) => { console.error(err); res.status(err.status || 500).json({ error: err.message }); }); sends JSON with error message and status code from error or 500.
  3. Final Answer:

    app.use((err, req, res, next) => { console.error(err); res.status(err.status || 500).json({ error: err.message }); }); -> Option D
  4. Quick Check:

    Proper error handler logs and sends JSON with status [OK]
Hint: Error handler needs 4 params, logs error, sends JSON with status [OK]
Common Mistakes:
  • Missing 'next' parameter in error handler
  • Not logging the error
  • Sending plain text instead of JSON
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard