Bird
0
0

You want to improve your REST API to handle unexpected errors gracefully and return a 500 Internal Server Error with a JSON message. Which approach is best?

hard📝 Application Q8 of 15
Rest API - HTTP Status Codes
You want to improve your REST API to handle unexpected errors gracefully and return a 500 Internal Server Error with a JSON message. Which approach is best?
AWrap all endpoint logic in try-catch blocks and return JSON error on exceptions.
BIgnore errors and let the server crash to restart automatically.
CReturn 200 OK with error details in the response body.
DReturn 404 Not Found for all errors.
Step-by-Step Solution
Solution:
  1. Step 1: Understand error handling best practices

    Graceful error handling means catching exceptions and returning meaningful responses.
  2. Step 2: Choose correct approach

    Wrapping logic in try-catch and returning JSON with 500 status is best practice.
  3. Final Answer:

    Wrap all endpoint logic in try-catch blocks and return JSON error on exceptions. -> Option A
  4. Quick Check:

    Try-catch with JSON 500 response = best practice [OK]
Quick Trick: Use try-catch to send JSON 500 error responses [OK]
Common Mistakes:
MISTAKES
  • Ignoring errors causing server crashes
  • Returning 200 status on errors confuses clients
  • Using 404 for server errors is incorrect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes