Bird
Raised Fist0
Expressframework~20 mins

Error response formatting in Express - Practice Problems & Coding Challenges

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

Practice

(1/5)
1. What is the main purpose of using res.status() in Express error handling?
easy
A. To redirect the user to an error page
B. To send the error message as plain text
C. To set the HTTP status code of the response
D. To log the error on the server console

Solution

  1. Step 1: Understand the role of res.status()

    This method sets the HTTP status code that tells the client what kind of response it is (success, error, etc.).
  2. Step 2: Differentiate from other response methods

    Unlike sending messages or logging, res.status() only sets the status code, which is essential for error handling.
  3. Final Answer:

    To set the HTTP status code of the response -> Option C
  4. Quick Check:

    res.status() sets status code [OK]
Hint: Remember: status() sets code, json() sends data [OK]
Common Mistakes:
  • Confusing status() with sending the error message
  • Using status() to log errors instead of setting code
  • Thinking status() redirects the user
2. Which of the following is the correct way to send a JSON error response with status 404 in Express?
easy
A. res.json(404).send({ error: 'Not found' })
B. res.sendStatus(404).json({ error: 'Not found' })
C. res.status(404).send('Not found')
D. res.status(404).json({ error: 'Not found' })

Solution

  1. Step 1: Identify correct chaining of methods

    In Express, res.status(code) sets the status, and res.json(object) sends JSON data.
  2. Step 2: Check each option's syntax

    res.status(404).json({ error: 'Not found' }) correctly chains status(404) and json({ error: 'Not found' }). Others misuse method order or types.
  3. Final Answer:

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

    Use status() then json() [OK]
Hint: Set status first, then send JSON with json() [OK]
Common Mistakes:
  • Calling json() before status()
  • Using sendStatus() then json() which is invalid
  • Sending plain text instead of JSON for errors
3. What will be the HTTP status code and JSON response body when this Express code runs?
app.get('/data', (req, res) => {
  if (!req.query.id) {
    return res.status(400).json({ error: 'ID is required' });
  }
  res.json({ id: req.query.id });
});
medium
A. Status 400 with body { error: 'ID is required' }
B. Status 200 with body { id: undefined }
C. Status 404 with body { error: 'Not found' }
D. Status 500 with empty body

Solution

  1. Step 1: Analyze the condition for missing query parameter

    If req.query.id is missing, the code returns a 400 status with JSON error message.
  2. Step 2: Understand the response when ID is missing

    The code sends res.status(400).json({ error: 'ID is required' }), so status is 400 and body contains the error message.
  3. Final Answer:

    Status 400 with body { error: 'ID is required' } -> Option A
  4. Quick Check:

    Missing id triggers 400 error JSON [OK]
Hint: Check conditions that trigger res.status() before sending response [OK]
Common Mistakes:
  • Assuming status 200 when id is missing
  • Confusing 400 with 404 status codes
  • Ignoring the early return on error
4. Identify the error in this Express error response code:
app.use((err, req, res, next) => {
  res.json({ error: err.message });
  res.status(500);
});
medium
A. The status code is set after sending the response
B. The error message should not be sent in JSON
C. The middleware is missing the next() call
D. The error handler should not have four parameters

Solution

  1. Step 1: Check order of response methods

    Calling res.json() sends the response immediately. Setting status after sending has no effect.
  2. Step 2: Correct order for setting status and sending response

    Status must be set before sending JSON: res.status(500).json(...).
  3. Final Answer:

    The status code is set after sending the response -> Option A
  4. Quick Check:

    Status before sending response [OK]
Hint: Always set status before sending response [OK]
Common Mistakes:
  • Setting status after res.json()
  • Not sending status at all
  • Misunderstanding middleware parameters
5. You want to send a 422 Unprocessable Entity error with a JSON body containing { error: 'Invalid input', fields: ['name', 'email'] }. Which Express code snippet correctly formats this error response?
hard
A. res.json({ error: 'Invalid input', fields: ['name', 'email'] }).status(422);
B. res.status(422).json({ error: 'Invalid input', fields: ['name', 'email'] });
C. res.sendStatus(422).json({ error: 'Invalid input', fields: ['name', 'email'] });
D. res.status(400).json({ error: 'Invalid input', fields: ['name', 'email'] });

Solution

  1. Step 1: Choose the correct status code and method order

    Use res.status(422) to set the correct HTTP status code before sending JSON.
  2. Step 2: Verify the JSON body matches the required error format

    The JSON object includes both error and fields keys as specified.
  3. Final Answer:

    res.status(422).json({ error: 'Invalid input', fields: ['name', 'email'] }); -> Option B
  4. Quick Check:

    Status 422 then json() with error details [OK]
Hint: Set status 422 first, then send JSON error object [OK]
Common Mistakes:
  • Calling json() before status()
  • Using wrong status code like 400
  • Using sendStatus() which sends no JSON body
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