0
0
Expressframework~20 mins

Async error handling in routes in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Error Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when an async route throws an error without next()?

Consider this Express route handler using async/await:

app.get('/data', async (req, res) => {
  const data = await fetchData();
  if (!data) throw new Error('No data found');
  res.json(data);
});

What will Express do if fetchData() returns null causing the error to be thrown?

AThe request will hang and eventually time out without a response.
BThe server will crash because the error is unhandled.
CExpress will catch the error and send a 500 response automatically.
DExpress will send a 404 Not Found response.
Attempts:
2 left
💡 Hint

Think about how Express handles errors in async functions without calling next().

📝 Syntax
intermediate
2:00remaining
Which option correctly passes async errors to Express error handler?

Given an async route, which code snippet correctly forwards errors to Express's error middleware?

A
app.get('/user', async (req, res, next) => {
  try {
    const user = await getUser();
    if (!user) throw new Error('User not found');
    res.json(user);
  } catch (err) {
    next(err);
  }
});
B
app.get('/user', (req, res, next) => {
  getUser().then(user => {
    if (!user) throw new Error('User not found');
    res.json(user);
  }).catch(next);
});
C
app.get('/user', async (req, res, next) => {
  const user = await getUser();
  if (!user) throw new Error('User not found');
  res.json(user);
});
D
app.get('/user', async (req, res) => {
  try {
    const user = await getUser();
    if (!user) throw new Error('User not found');
    res.json(user);
  } catch (err) {
    res.status(500).send(err.message);
  }
});
Attempts:
2 left
💡 Hint

Look for the option that uses next(err) inside a try/catch block.

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

Examine this Express route:

app.get('/items', async (req, res, next) => {
  fetchItems().then(items => {
    if (!items) throw new Error('No items');
    res.json(items);
  });
});

Why won't errors thrown inside then() be caught by Express's error handler?

ABecause Express only catches errors thrown synchronously, not inside promises.
BBecause the async function does not await the promise, errors inside then() are unhandled.
CBecause the route handler is missing the next parameter.
DBecause fetchItems() is not returning a promise.
Attempts:
2 left
💡 Hint

Consider how async functions and promises interact with error propagation.

🧠 Conceptual
advanced
2:00remaining
What is the benefit of using a wrapper function for async route handlers?

Why do many Express apps use a wrapper like const wrap = fn => (req, res, next) => fn(req, res, next).catch(next); around async route handlers?

AIt converts async functions into synchronous ones.
BIt improves performance by caching route responses.
CIt prevents the route handler from running multiple times.
DIt automatically catches errors from async functions and forwards them to Express error middleware.
Attempts:
2 left
💡 Hint

Think about how error handling is simplified with this pattern.

state_output
expert
2:00remaining
What is the response status and body when an async route calls next() with an error?

Given this Express setup:

app.get('/fail', async (req, res, next) => {
  try {
    await Promise.reject(new Error('Failed'));
  } catch (err) {
    next(err);
  }
});

app.use((err, req, res, next) => {
  res.status(400).json({ error: err.message });
});

What will the client receive when requesting /fail?

AStatus 500 with empty response body
BStatus 200 with JSON body {"error":"Failed"}
CStatus 400 with JSON body {"error":"Failed"}
DNo response, request times out
Attempts:
2 left
💡 Hint

Look at the error middleware and the status code it sets.