0
0
Expressframework~20 mins

Error response formatting in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Error Response Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Express error handler?
Consider this Express error handling middleware. What JSON response will the client receive if an error with message 'Invalid input' and status 400 is passed to next()?
Express
app.use((err, req, res, next) => {
  res.status(err.status || 500).json({
    error: {
      message: err.message || 'Internal Server Error',
      status: err.status || 500
    }
  });
});
A{"error":{"message":"Invalid input","status":400}}
B{"error":{"message":"Internal Server Error","status":500}}
C{"message":"Invalid input","status":400}
D{"error":"Invalid input"}
Attempts:
2 left
💡 Hint
Look at how the error object properties are used in the JSON response.
📝 Syntax
intermediate
2:00remaining
Which option causes a syntax error in Express error middleware?
Identify which error handler code snippet will cause a syntax error.
A
app.use((err, req, res, next) => {
  res.status(500).json({ error: err.message });
});
B
app.use((err, req, res, next) => {
  res.status(500).json({ error: err.message )};
});
C
;)}
;)} egassem.rre :rorre {(nosj.)005(sutats.ser  
{ >= )txen ,ser ,qer ,rre((esu.ppa
D
app.use(function(err, req, res, next) {
  res.status(500).json({ error: err.message });
});
Attempts:
2 left
💡 Hint
Check the brackets and parentheses carefully.
state_output
advanced
2:00remaining
What is the HTTP status code sent by this error handler?
Given this Express error middleware, what status code will be sent if err.status is undefined?
Express
app.use((err, req, res, next) => {
  res.status(err.status || 404).json({
    error: err.message || 'Not Found'
  });
});
A200
B500
C404
Dundefined
Attempts:
2 left
💡 Hint
Look at the fallback value used with the OR operator.
🔧 Debug
advanced
2:00remaining
Why does this error handler not send a JSON response?
This Express error handler does not send a JSON response as expected. What is the cause?
Express
app.use((err, req, res, next) => {
  res.status(500);
  res.json({ error: err.message });
  next();
});
ACalling next() after res.json() causes the response to be sent twice, breaking JSON output.
BMissing return statement after res.status(500) prevents response from sending.
Cres.json() is called without setting status, so default 200 is sent.
DError handler must have 3 parameters, not 4.
Attempts:
2 left
💡 Hint
Think about what happens when next() is called after sending a response.
🧠 Conceptual
expert
3:00remaining
Which option correctly formats an error response with a custom error code and message in Express?
You want to send a JSON error response with a custom code and message using Express error middleware. Which code snippet does this correctly?
A
app.use((err, req, res, next) => {
  res.json({
    error: {
      code: err.code,
      message: err.message
    }
  });
});
B
app.use((err, req, res, next) => {
  res.status(400).json({
    code: err.code || 'ERR_CUSTOM',
    message: err.message || 'Custom error'
  });
});
C
app.use((err, req, res, next) => {
  res.status(err.statusCode).json({
    error: err.message
  });
});
D
app.use((err, req, res, next) => {
  res.status(err.statusCode || 400).json({
    error: {
      code: err.code || 'ERR_CUSTOM',
      message: err.message || 'Custom error'
    }
  });
});
Attempts:
2 left
💡 Hint
Check how status code and error details are included in the response.