Bird
Raised Fist0
Node.jsframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using res.status() in Node.js error handling?
easy
A. To log errors on the server console
B. To send a success message to the client
C. To restart the server automatically
D. To set the HTTP status code for the response

Solution

  1. Step 1: Understand HTTP status codes

    HTTP status codes tell the client if a request succeeded or failed.
  2. Step 2: Role of res.status()

    This method sets the HTTP status code in the response, indicating success or error.
  3. Final Answer:

    To set the HTTP status code for the response -> Option D
  4. Quick Check:

    res.status() sets HTTP status code [OK]
Hint: Remember: status sets code, not message or logs [OK]
Common Mistakes:
  • Confusing status with sending messages
  • Thinking it logs errors
  • Assuming it restarts the server
2. Which of the following is the correct way to send a JSON error response with status 404 in Express?
easy
A. res.send(404).json({ error: 'Not found' })
B. res.status(404).json({ error: 'Not found' })
C. res.json(404, { error: 'Not found' })
D. res.error(404).send({ error: 'Not found' })

Solution

  1. Step 1: Check Express response methods

    Use res.status(code) to set status, then json() to send JSON.
  2. Step 2: Validate syntax correctness

    Only res.status(404).json({ error: 'Not found' }) is valid syntax.
  3. Final Answer:

    res.status(404).json({ error: 'Not found' }) -> Option B
  4. Quick Check:

    Set status then send JSON [OK]
Hint: Use res.status(code).json(object) for error responses [OK]
Common Mistakes:
  • Calling res.send() before json()
  • Passing status code inside json()
  • Using non-existent res.error() method
3. Given this Express error handler code:
app.use((err, req, res, next) => {
  res.status(500).json({
    message: err.message,
    code: 'SERVER_ERROR'
  });
});

What will the client receive if an error with message 'Database down' occurs?
medium
A. {"message":"Database down","code":"SERVER_ERROR"} with status 500
B. {"error":"Database down"} with status 200
C. Plain text 'Database down' with status 500
D. Empty response with status 500

Solution

  1. Step 1: Analyze the error handler response

    The handler sets status 500 and sends JSON with message and code fields.
  2. Step 2: Match error message and response

    The JSON includes the error message 'Database down' and code 'SERVER_ERROR'.
  3. Final Answer:

    {"message":"Database down","code":"SERVER_ERROR"} with status 500 -> Option A
  4. Quick Check:

    Error message and code sent as JSON with 500 status [OK]
Hint: Error handlers send JSON with message and code [OK]
Common Mistakes:
  • Assuming status 200 by default
  • Expecting plain text instead of JSON
  • Missing error code in response
4. Identify the error in this Express error response code:
app.use((err, req, res) => {
  res.status(400).json({ message: err.message });
});
medium
A. Missing the next parameter in the middleware function
B. Using res.status() instead of res.sendStatus()
C. Not calling res.end() after json()
D. Incorrect status code for errors

Solution

  1. Step 1: Check Express error middleware signature

    Error middleware must have four parameters: err, req, res, next.
  2. Step 2: Identify missing parameter

    The code has only three parameters, missing next, so Express won't recognize it as error handler.
  3. Final Answer:

    Missing the next parameter in the middleware function -> Option A
  4. Quick Check:

    Error middleware needs 4 params: err, req, res, next [OK]
Hint: Error middleware must have 4 parameters [OK]
Common Mistakes:
  • Leaving out next parameter
  • Confusing res.status() with res.sendStatus()
  • Thinking json() needs res.end()
5. You want to send a detailed error response with a custom code and message, but only if the error has a statusCode property. Otherwise, send a 500 status with a generic message. Which code snippet correctly implements this in Express?
hard
A. app.use((err, req, res, next) => { if (err.statusCode) { res.json({ code: err.statusCode, message: err.message }); } else { res.status(500).send('Error'); } });
B. app.use((err, req, res) => { res.status(err.statusCode).json({ message: err.message }); });
C. app.use((err, req, res, next) => { const status = err.statusCode || 500; const message = err.message || 'Internal Server Error'; res.status(status).json({ code: status, message }); });
D. app.use((err, req, res, next) => { res.status(500).json({ code: 'ERROR', message: 'Something went wrong' }); });

Solution

  1. Step 1: Check for error statusCode and message

    Use err.statusCode if present, else default to 500; similarly for message.
  2. Step 2: Send JSON response with status and message

    Set status with res.status(status) and send JSON with code and message fields.
  3. Final Answer:

    app.use((err, req, res, next) => { const status = err.statusCode || 500; const message = err.message || 'Internal Server Error'; res.status(status).json({ code: status, message }); }); -> Option C
  4. Quick Check:

    Use defaults and send JSON with status and message [OK]
Hint: Use || to default status and message in error handler [OK]
Common Mistakes:
  • Missing next parameter in middleware
  • Not defaulting status or message
  • Sending plain text instead of JSON
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