Bird
Raised Fist0
Node.jsframework~20 mins

Error-handling middleware in Node.js - Practice Problems & Coding Challenges

Choose your learning style10 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 affect request flow in Express?

Consider an Express app with normal middleware and error-handling middleware. What happens when an error is passed to next()?

Node.js
const express = require('express');
const app = express();

app.use((req, res, next) => {
  if (req.query.fail) {
    next(new Error('Failure triggered'));
  } else {
    next();
  }
});

app.use((req, res, next) => {
  res.send('Success');
});

app.use((err, req, res, next) => {
  res.status(500).send('Error caught: ' + err.message);
});
AThe error-handling middleware runs only if <code>res.send()</code> is not called first.
BThe error is ignored and the normal middleware continues to run, sending 'Success'.
CThe app crashes immediately when <code>next()</code> is called with an error.
DIf <code>next()</code> is called with an error, the error-handling middleware runs and sends a 500 response.
Attempts:
2 left
💡 Hint

Think about how Express decides which middleware to run when next() receives an error.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this error-handling middleware

Which option contains a syntax error in defining error-handling middleware in Express?

Node.js
app.use(function(err, req, res) {
  res.status(500).send('Error: ' + err.message);
});
AMissing <code>next</code> parameter in the middleware function signature.
BUsing <code>function</code> keyword instead of arrow function.
CMiddleware function has four parameters but <code>err</code> is not first.
DCalling <code>res.status()</code> without <code>send()</code>.
Attempts:
2 left
💡 Hint

Error-handling middleware must have exactly four parameters.

🔧 Debug
advanced
2:00remaining
Why does this error-handling middleware never run?

Given this Express app, why does the error-handling middleware never respond?

Node.js
const express = require('express');
const app = express();

app.use((req, res, next) => {
  next(new Error('Oops'));
});

app.use((req, res, next) => {
  res.send('Hello');
});

app.use((req, res, next, err) => {
  res.status(500).send('Error: ' + err.message);
});
AThe error-handling middleware is missing the <code>next</code> parameter.
BThe error-handling middleware has parameters in the wrong order; <code>err</code> must be first.
CThe error-handling middleware is placed before the normal middleware.
DThe error-handling middleware uses arrow function syntax which is not supported.
Attempts:
2 left
💡 Hint

Check the order of parameters in error-handling middleware.

❓ state_output
advanced
2:00remaining
What is the response status and body when an error is thrown inside async middleware?

Consider this Express async middleware that throws an error. What response does the client receive?

Node.js
const express = require('express');
const app = express();

app.use(async (req, res, next) => {
  throw new Error('Async failure');
});

app.use((err, req, res, next) => {
  res.status(500).send('Caught: ' + err.message);
});
AThe app crashes with an unhandled promise rejection error.
BThe client receives a 500 status with body 'Caught: Async failure'.
CThe client receives no response and the request times out.
DThe client receives a 200 status with empty body.
Attempts:
2 left
💡 Hint

Think about how Express handles errors thrown inside async functions.

🧠 Conceptual
expert
2:00remaining
Which statement about error-handling middleware in Express is TRUE?

Choose the correct statement about error-handling middleware behavior in Express.

ACalling <code>next()</code> without arguments inside error-handling middleware passes control to the next error handler.
BError-handling middleware must be the last middleware added to the app to catch all errors.
CError-handling middleware is identified by having exactly four parameters: <code>err, req, res, next</code>.
DIf an error occurs in normal middleware, Express automatically skips all remaining middleware and sends a 500 response.
Attempts:
2 left
💡 Hint

Recall how Express detects error-handling middleware.

Practice

(1/5)
1. What is the correct number of parameters for an error-handling middleware function in Node.js Express?
easy
A. One parameter: err
B. Three parameters: req, res, next
C. Two parameters: req, res
D. Four parameters: err, req, res, next

Solution

  1. Step 1: Recall the error middleware signature

    Error-handling middleware in Express always has four parameters: err, req, res, and next.
  2. Step 2: Understand the role of each parameter

    The first parameter is the error object, followed by the request, response, and next function to pass control.
  3. Final Answer:

    Four parameters: err, req, res, next -> Option D
  4. Quick Check:

    Error middleware = 4 parameters [OK]
Hint: Error middleware always has 4 parameters: err, req, res, next [OK]
Common Mistakes:
  • Using 3 parameters like normal middleware
  • Forgetting the err parameter
  • Placing error middleware before routes
2. Which of the following is the correct syntax to define an error-handling middleware in Express?
easy
A. app.use((err, req, res, next) => { /* handle error */ })
B. app.use((req, res, next, err) => { /* handle error */ })
C. app.use((err, req, next, res) => { /* handle error */ })
D. app.use((req, err, res, next) => { /* handle error */ })

Solution

  1. Step 1: Check parameter order in error middleware

    The correct order is err, req, res, next for error-handling middleware.
  2. Step 2: Verify the syntax matches Express pattern

    app.use((err, req, res, next) => { /* handle error */ }) matches the correct order and syntax for error middleware.
  3. Final Answer:

    app.use((err, req, res, next) => { /* handle error */ }) -> Option A
  4. Quick Check:

    Correct parameter order = err, req, res, next [OK]
Hint: Error middleware params start with err, then req, res, next [OK]
Common Mistakes:
  • Mixing parameter order
  • Placing err parameter last or in the middle
  • Using normal middleware syntax for error middleware
3. Consider this Express app code snippet:
app.get('/test', (req, res) => {
throw new Error('Fail');
});

app.use((err, req, res, next) => {
res.status(500).send('Error caught');
});

What will the client receive when requesting /test?
medium
A. A 404 Not Found error
B. A 500 status with 'Error caught' message
C. A 200 status with no response body
D. The server crashes with an uncaught exception

Solution

  1. Step 1: Understand error thrown in route handler

    The route throws an error which Express catches and passes to error middleware.
  2. Step 2: Error middleware sends 500 status and message

    The error middleware sets status 500 and sends 'Error caught' to the client.
  3. Final Answer:

    A 500 status with 'Error caught' message -> Option B
  4. Quick Check:

    Error thrown -> error middleware response [OK]
Hint: Thrown errors go to error middleware response [OK]
Common Mistakes:
  • Expecting 200 OK despite error
  • Thinking server crashes without middleware
  • Confusing 404 with error middleware response
4. What is wrong with this error-handling middleware code?
app.use((err, req, res) => {
res.status(500).send('Error!');
});
medium
A. It should have only two parameters
B. It should be placed before routes
C. It is missing the next parameter
D. It should not use res.status

Solution

  1. Step 1: Check error middleware parameters

    Error middleware must have four parameters: err, req, res, next.
  2. Step 2: Identify missing parameter

    The code is missing the next parameter, which is required even if unused.
  3. Final Answer:

    It is missing the next parameter -> Option C
  4. Quick Check:

    Error middleware needs 4 params including next [OK]
Hint: Error middleware always needs 4 parameters, including next [OK]
Common Mistakes:
  • Omitting next parameter
  • Placing error middleware before routes
  • Using wrong parameter order
5. You want to log errors and then send a response in your Express app. Which is the best way to write the error-handling middleware?
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something broke!');
});

What is a potential problem with this code if you add more error middleware after it?
hard
A. It does not call next(), so later error middleware won't run
B. It calls next() without an error, causing infinite loop
C. It sends response twice causing an error
D. It should not log errors in middleware

Solution

  1. Step 1: Analyze middleware flow after sending response

    Once res.send is called, response is sent and middleware usually ends.
  2. Step 2: Understand next() usage in error middleware

    If next() is not called, any following error middleware won't execute.
  3. Step 3: Identify problem if more error middleware exists

    Without calling next(err), later error middleware is skipped, possibly missing logging or handling.
  4. Final Answer:

    It does not call next(), so later error middleware won't run -> Option A
  5. Quick Check:

    Missing next() call skips later error middleware [OK]
Hint: Call next(err) to continue error middleware chain [OK]
Common Mistakes:
  • Not calling next() after sending response
  • Calling next() without error causing loops
  • Logging errors outside middleware
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