Add Robust Error Handling to Any Code
Add comprehensive error handling, retry logic, and graceful degradation to any function or module.
The Prompt
Add robust error handling to the following code. Include: 1. Try/catch blocks at appropriate levels 2. Specific error types (not just catching `Error`) 3. Retry logic with exponential backoff for transient failures 4. Meaningful error messages that aid debugging 5. Graceful degradation — what should the user/caller get when things fail? 6. Logging at appropriate severity levels (debug, warn, error) Code to improve: ``` [PASTE YOUR CODE] ``` Context: [e.g., This is an API client that calls external services. We want to retry network errors but not 4xx responses. Timeouts should be 5 seconds.]
Example Output
Added a custom `ApiError` class hierarchy (NetworkError, AuthError, RateLimitError), retry logic with exponential backoff (max 3 retries, 2x multiplier, up to 8s), circuit breaker pattern for repeated failures, and structured logging with request IDs for traceability.
FAQ
Which AI model is best for Add Robust Error Handling to Any Code?
Claude Sonnet 4 — excellent at thinking through failure modes comprehensively.
How do I use the Add Robust Error Handling to Any Code prompt?
Copy the prompt, replace the [BRACKETED] placeholders with your specific information, and paste into your preferred AI assistant (ChatGPT, Claude, Gemini, etc.). Added a custom `ApiError` class hierarchy (NetworkError, AuthError, RateLimitError), retry logic with exponential backoff (max 3 retries, 2x multiplier, up to 8s), circuit breaker pattern for repeated failures, and structured logging with request IDs for traceability.
Model Recommendation
Claude Sonnet 4 — excellent at thinking through failure modes comprehensively.