0
0
Expressframework~20 mins

Why error handling is critical in Express - Challenge Your Understanding

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!
🧠 Conceptual
intermediate
2:00remaining
Why is error handling important in Express apps?

Imagine you build a web app with Express. What is the main reason to add error handling middleware?

ATo catch errors and send friendly messages instead of crashing the server
BTo automatically fix bugs in the code
CTo speed up the app by skipping error checks
DTo prevent users from accessing any routes
Attempts:
2 left
💡 Hint

Think about what happens if an error occurs but is not caught.

component_behavior
intermediate
2:00remaining
What happens when an error is thrown without error middleware?

Consider this Express route:

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

What will the server do if no error handling middleware is added?

Express
app.get('/data', (req, res) => { throw new Error('Oops!'); });
AThe server automatically retries the request
BThe server ignores the error and sends a success response
CThe error is logged but the response is sent normally
DThe server crashes or sends a generic error response and stops processing
Attempts:
2 left
💡 Hint

Think about what happens when an exception is not caught in Node.js.

📝 Syntax
advanced
2:00remaining
Identify the correct error handling middleware syntax

Which of these is the correct way to define error handling middleware in Express?

Aapp.use((err, req, res, next) => { res.status(500).send('Error!'); });
Bapp.use((req, res, next) => { res.status(500).send('Error!'); });
Capp.get((err, req, res) => { res.status(500).send('Error!'); });
Dapp.use((err, req, res) => { res.status(500).send('Error!'); });
Attempts:
2 left
💡 Hint

Error middleware must have four parameters: err, req, res, next.

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

Look at this code snippet:

app.use((err, req, res, next) => { res.status(500).send('Caught error'); });
app.use((req, res, next) => { throw new Error('Fail'); });

Why does the error handler not catch the error?

Express
app.use((err, req, res, next) => { res.status(500).send('Caught error'); });
app.use((req, res, next) => { throw new Error('Fail'); });
ABecause the error is thrown in synchronous middleware without calling next(err)
BBecause the error handler is defined before the throwing middleware
CBecause the error handler has wrong parameter order
DBecause Express does not support error handling middleware
Attempts:
2 left
💡 Hint

Order of middleware matters in Express.

state_output
expert
2:00remaining
What is the response status and body when an error is passed to next()?

Given this Express code:

app.get('/test', (req, res, next) => {
  next(new Error('Test error'));
});

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

What will the client receive when requesting /test?

Express
app.get('/test', (req, res, next) => {
  next(new Error('Test error'));
});

app.use((err, req, res, next) => {
  res.status(400).json({ message: err.message });
});
ANo response, request hangs
BStatus 500 with plain text 'Internal Server Error'
CStatus 400 with JSON body {"message":"Test error"}
DStatus 200 with empty body
Attempts:
2 left
💡 Hint

What does calling next(error) do in Express?