0
0
Node.jsframework~20 mins

Error response formatting in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Error Response 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 error handler?
Consider this Express.js error handling middleware. What JSON response will the client receive if an error with message 'Invalid input' and status 400 is passed?
Node.js
app.use((err, req, res, next) => {
  res.status(err.status || 500).json({
    error: {
      message: err.message || 'Internal Server Error',
      code: err.status || 500
    }
  });
});
A{"error":{"message":"Internal Server Error","code":500}}
B{"error":{"msg":"Invalid input","status":400}}
C{"message":"Invalid input","code":400}
D{"error":{"message":"Invalid input","code":400}}
Attempts:
2 left
💡 Hint
Look at how the error message and status are accessed and structured in the JSON response.
Predict Output
intermediate
2:00remaining
What error does this Express error handler cause?
What error will this Express error handler cause when an error is passed to it?
Node.js
app.use((err, req, res, next) => {
  res.status(err.status).json({
    error: err.message
  });
});
ANo error, sends JSON with error message and undefined status code
BTypeError: Cannot read property 'status' of undefined
CReferenceError: err is not defined
DSyntaxError: Unexpected token
Attempts:
2 left
💡 Hint
Consider what happens if err.status is undefined.
component_behavior
advanced
2:00remaining
Which option correctly formats a JSON error response with stack trace only in development?
You want to send an error response JSON that includes the error message always, but includes the stack trace only if NODE_ENV is 'development'. Which code snippet does this correctly?
Ares.status(err.status || 500).json({ message: err.message });
Bres.status(err.status || 500).json({ message: err.message, stack: process.env.NODE_ENV === 'production' ? err.stack : undefined });
Cres.status(err.status || 500).json({ message: err.message, stack: process.env.NODE_ENV === 'development' ? err.stack : undefined });
Dres.status(err.status || 500).json({ message: err.message, stack: err.stack });
Attempts:
2 left
💡 Hint
Check the environment variable condition for including the stack trace.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in this error response code?
Identify the option that will cause a syntax error when used as an Express error handler response.
Node.js
app.use((err, req, res, next) => {
  res.status(500).json({
    error: err.message,
    details: err.stack
  });
});
Ares.status(500).json({ error: err.message, details: err.stack, })
Bres.status(500).json({ error: err.message details: err.stack })
Cres.status(500).json({ error: err.message, details: err.stack })
Dres.status(500).json({ error: err.message + details: err.stack })
Attempts:
2 left
💡 Hint
Look for missing commas or invalid object syntax.
🔧 Debug
expert
3:00remaining
Why does this Express error handler send a 200 status instead of 500?
Given this error handler, why does the client receive a 200 OK status instead of 500 when an error occurs? app.use((err, req, res, next) => { res.status(err.status).json({ error: err.message }); });
ABecause err.status is undefined, res.status(undefined) defaults to 200 OK in Express
BBecause the error handler is missing next() call to propagate the error
CBecause res.status() always sends 200 unless explicitly called with 500
DBecause JSON responses always override status codes to 200
Attempts:
2 left
💡 Hint
Check what happens if err.status is not set.