0
0
Node.jsframework~20 mins

Centralized error handling in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Centralized Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What is the main benefit of centralized error handling in Node.js?

Consider a Node.js app with many routes and asynchronous operations. What is the main benefit of using centralized error handling middleware?

AIt allows handling all errors in one place, reducing repeated code and improving maintainability.
BIt automatically fixes all runtime errors without developer intervention.
CIt prevents errors from occurring by validating all inputs before processing.
DIt speeds up the application by skipping error checks in individual routes.
Attempts:
2 left
💡 Hint

Think about how many places you would need to write error handling code without centralization.

component_behavior
intermediate
1:30remaining
What will this Express error middleware output when an error occurs?

Given this Express error middleware, what will the client receive if an error with message 'File not found' is passed?

Node.js
app.use((err, req, res, next) => {
  res.status(404).json({ error: err.message });
});
A{"error":"Internal Server Error"}
B{"message":"File not found"}
C{"error":"File not found"}
D404 File not found (plain text)
Attempts:
2 left
💡 Hint

Look at the JSON key used in the response.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this centralized error handler setup

Which option contains the correct syntax for an Express centralized error handler?

Node.js
app.use((err, req, res, next) => {
  res.status(500).send('Server error');
});
Aapp.use((err, req, res, next) => { res.status(500).send('Server error'); });
Bapp.use((err, req, res) => { res.status(500).send('Error'); });
Capp.use((error, request, response, next) => { response.status(500).send('Error'); });
Dapp.use((err, req, res, next) => { res.status(500).send('Server error') });
Attempts:
2 left
💡 Hint

Check the number and order of parameters in the middleware function.

🔧 Debug
advanced
2:00remaining
Why does this centralized error handler not catch errors from async functions?

Consider this Express route and error handler. Why does the error handler not catch the thrown error?

Node.js
app.get('/data', async (req, res) => {
  throw new Error('Async failure');
});

app.use((err, req, res, next) => {
  res.status(500).json({ error: err.message });
});
ABecause the error handler must be registered before the route.
BBecause the error handler is missing the 'next' parameter.
CBecause Express does not support error handling for GET routes.
DBecause errors thrown inside async functions must be passed to next() explicitly or caught with a wrapper.
Attempts:
2 left
💡 Hint

Think about how Express handles errors in async functions.

state_output
expert
2:30remaining
What is the output sequence when multiple errors are passed through next() in Express?

Given these middleware functions, what will be the final response sent to the client?

Node.js
app.use((req, res, next) => {
  next(new Error('First error'));
});

app.use((err, req, res, next) => {
  if (err.message === 'First error') {
    next(new Error('Second error'));
  } else {
    res.status(500).send(err.message);
  }
});

app.use((err, req, res, next) => {
  res.status(500).send(err.message);
});
AThe client receives no response and the request hangs.
BThe client receives 'Second error' with status 500.
CThe client receives 'First error' with status 500.
DThe client receives a 404 Not Found error.
Attempts:
2 left
💡 Hint

Follow the error passed through next() calls step by step.