0
0
Node.jsframework~20 mins

Error-handling middleware in Node.js - 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!
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.