Bird
Raised Fist0
Node.jsframework~5 mins

Error response formatting in Node.js - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the purpose of error response formatting in Node.js APIs?
Error response formatting helps send clear, consistent messages to clients when something goes wrong. It makes debugging easier and improves user experience by providing understandable error details.
Click to reveal answer
beginner
Which HTTP status code is commonly used for a generic server error?
The HTTP status code 500 is used for generic server errors, indicating that something went wrong on the server side.
Click to reveal answer
intermediate
Why should error responses include a consistent JSON structure?
Consistent JSON structure allows clients to easily parse and handle errors programmatically. It also helps frontend developers display meaningful messages to users.
Click to reveal answer
beginner
Show a simple example of a JSON error response format.
A simple JSON error response might look like: {"error": {"message": "Not Found", "code": 404}}. This clearly tells the client what went wrong and the error code.
Click to reveal answer
intermediate
What is a good practice when sending error details in production environments?
In production, avoid sending sensitive error details to clients. Instead, send a generic message and log the detailed error on the server for developers to review.
Click to reveal answer
Which HTTP status code indicates a client error due to bad request?
A400
B500
C200
D302
What is the main benefit of formatting error responses as JSON?
AIt speeds up the server
BIt makes errors easier to read and handle by clients
CIt hides all error details
DIt changes the HTTP status code automatically
Which of these is NOT a good practice for error response formatting?
ALogging detailed errors on the server
BSending consistent error structure
CUsing proper HTTP status codes
DIncluding sensitive server details in production errors
What should an error response include besides the HTTP status code?
AA clear error message
BThe full server stack trace
CClient's IP address
DUser's password
In Node.js, which method is commonly used to send a JSON error response?
Aconsole.log()
Bres.sendFile()
Cres.json()
Dprocess.exit()
Explain why consistent error response formatting is important in Node.js APIs.
Think about how clients and developers benefit from predictable error messages.
You got /4 concepts.
    Describe best practices for sending error responses in production environments.
    Consider security and user experience.
    You got /4 concepts.

      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