0
0
Expressframework~20 mins

Synchronous error handling in Express - 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!
Predict Output
intermediate
2:00remaining
What is the output of this Express middleware error handling?
Consider this Express middleware function. What will the server respond with when a synchronous error occurs?
Express
const express = require('express');
const app = express();

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

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

app.listen(3000);
AThe server responds with status 500 and body 'Error caught: Synchronous error'.
BThe server crashes and stops running.
CThe server responds with status 200 and an empty body.
DThe server responds with status 404 Not Found.
Attempts:
2 left
💡 Hint
Think about how Express handles errors thrown inside middleware.
Predict Output
intermediate
2:00remaining
What happens if next() is called with an error synchronously?
Look at this Express route handler. What will the server respond with when the route is accessed?
Express
const express = require('express');
const app = express();

app.get('/', (req, res, next) => {
  next(new Error('Error passed to next'));
});

app.use((err, req, res, next) => {
  res.status(500).send('Caught error: ' + err.message);
});

app.listen(3000);
AThe server responds with status 200 and body 'OK'.
BThe server responds with status 404 Not Found.
CThe server responds with status 500 and body 'Caught error: Error passed to next'.
DThe server crashes with an uncaught exception.
Attempts:
2 left
💡 Hint
What does calling next() with an error do in Express?
component_behavior
advanced
2:30remaining
Which middleware order causes synchronous errors to be caught correctly?
Given these middleware registrations, which order ensures synchronous errors thrown in routes are caught by the error handler?
Express
const express = require('express');
const app = express();

function errorHandler(err, req, res, next) {
  res.status(500).send('Error: ' + err.message);
}

function routeHandler(req, res) {
  throw new Error('Oops');
}

// Options show different orders of app.use and app.get calls
A
app.use(errorHandler);
app.get('/', routeHandler);
B
app.get('/', routeHandler);
app.use(errorHandler);
C
app.use(routeHandler);
app.use(errorHandler);
Dapp.get('/', routeHandler);
Attempts:
2 left
💡 Hint
Error-handling middleware must be registered after routes to catch their errors.
📝 Syntax
advanced
2:00remaining
Which error-handling middleware signature is correct for synchronous errors?
Which of these middleware functions is correctly defined to catch synchronous errors in Express?
A(req, res) => { throw new Error('Error'); }
B(req, res, next) => { res.status(500).send('Error'); }
C(err, req, res) => { res.status(500).send(err.message); }
D(err, req, res, next) => { res.status(500).send(err.message); }
Attempts:
2 left
💡 Hint
Error-handling middleware must have four parameters.
🔧 Debug
expert
3:00remaining
Why does this synchronous error crash the Express server?
This code throws a synchronous error inside a route, but the server crashes instead of sending a response. Why?
Express
const express = require('express');
const app = express();

app.get('/', (req, res) => {
  throw new Error('Crash!');
});

app.listen(3000);
ANo error-handling middleware is defined to catch the thrown error.
BThe error is asynchronous and cannot be caught by Express.
CThe route handler uses next() incorrectly.
DThe server port 3000 is already in use.
Attempts:
2 left
💡 Hint
Express needs special middleware to catch errors and prevent crashes.