Bird
Raised Fist0
Expressframework~20 mins

Synchronous error handling in Express - 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!
❓ 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.

Practice

(1/5)
1. In Express, how do you handle synchronous errors inside a route handler?
easy
A. Use setTimeout to catch errors asynchronously
B. Use async/await without try...catch
C. Ignore errors and let the server crash
D. Use a try...catch block and call next(error) inside catch

Solution

  1. Step 1: Understand synchronous error handling

    Synchronous errors happen immediately and can be caught with try...catch blocks.
  2. Step 2: Pass error to Express error handler

    Inside catch, call next(error) to forward the error to Express's error middleware.
  3. Final Answer:

    Use a try...catch block and call next(error) inside catch -> Option D
  4. Quick Check:

    try...catch + next(error) = A [OK]
Hint: Catch sync errors with try...catch and call next(error) [OK]
Common Mistakes:
  • Not using try...catch for synchronous errors
  • Calling next() without an error object
  • Using async/await without try...catch for sync errors
2. Which of the following is the correct syntax to pass a synchronous error to the next middleware in Express?
easy
A. next(new Error('Something went wrong'))
B. throw new Error('Something went wrong')
C. res.send(new Error('Something went wrong'))
D. console.error('Something went wrong')

Solution

  1. Step 1: Identify how to forward errors in Express

    Express expects errors to be passed to next() as an argument to trigger error middleware.
  2. Step 2: Check syntax correctness

    Calling next(new Error('...')) correctly passes the error object to Express.
  3. Final Answer:

    next(new Error('Something went wrong')) -> Option A
  4. Quick Check:

    next(error) syntax = B [OK]
Hint: Use next(error) to forward errors in Express [OK]
Common Mistakes:
  • Throwing error without try...catch in sync code
  • Sending error object in response instead of next()
  • Logging error without forwarding it
3. What will be the output when this Express route is called?
app.get('/test', (req, res, next) => {
  try {
    throw new Error('Fail');
  } catch (err) {
    next(err);
  }
});

app.use((err, req, res, next) => {
  res.status(500).send(err.message);
});
medium
A. The server crashes with uncaught exception
B. The client receives a 200 status with empty body
C. The client receives a 500 status with message 'Fail'
D. The client receives a 404 Not Found error

Solution

  1. Step 1: Analyze error throwing and catching

    The route throws an error inside try, caught immediately, then passed to next(err).
  2. Step 2: Understand error middleware response

    The error middleware sends a 500 status with the error message 'Fail' to the client.
  3. Final Answer:

    The client receives a 500 status with message 'Fail' -> Option C
  4. Quick Check:

    next(err) triggers error middleware = A [OK]
Hint: Errors passed to next() trigger error middleware response [OK]
Common Mistakes:
  • Assuming error crashes server without middleware
  • Expecting 200 status despite error
  • Confusing error middleware with normal middleware
4. Identify the error in this Express route for synchronous error handling:
app.get('/error', (req, res, next) => {
  throw new Error('Oops');
  next();
});
medium
A. No error, code works fine
B. Error is thrown but not caught or passed to next()
C. Error middleware is missing
D. next() is called after throw, which is unreachable

Solution

  1. Step 1: Check error throwing without try...catch

    The error is thrown synchronously but not caught, so Express cannot handle it properly.
  2. Step 2: Understand consequence of uncaught error

    Without try...catch and calling next(err), the server may crash or not handle error gracefully.
  3. Final Answer:

    Error is thrown but not caught or passed to next() -> Option B
  4. Quick Check:

    Throw without catch + next(err) = D [OK]
Hint: Always catch sync errors and call next(error) [OK]
Common Mistakes:
  • Throwing error without try...catch
  • Calling next() after throw (unreachable code)
  • Assuming Express auto-catches thrown errors
5. You want to handle synchronous errors in multiple routes without repeating try...catch blocks. Which Express feature helps you achieve this cleanly?
hard
A. Create a wrapper function that catches errors and calls next(err)
B. Use setTimeout to delay error throwing
C. Call res.end() immediately to avoid errors
D. Use global variables to store errors

Solution

  1. Step 1: Recognize repetitive try...catch problem

    Wrapping every route in try...catch is repetitive and cluttered.
  2. Step 2: Use a wrapper function to centralize error catching

    A wrapper function can run the route handler and catch sync errors, forwarding them with next(err).
  3. Final Answer:

    Create a wrapper function that catches errors and calls next(err) -> Option A
  4. Quick Check:

    Wrapper function for sync error handling = C [OK]
Hint: Use wrapper functions to catch and forward sync errors [OK]
Common Mistakes:
  • Trying to delay errors with setTimeout
  • Ignoring errors by ending response early
  • Using global variables for error handling
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