Prompt · Software Engineers
Robust API Error Handling
Use this when you need to design or improve error handling for API responses in your code.
How to use it
- Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
- Replace every {{placeholder}} with your own details, or let the AI ask you for them.
- Use the follow-ups below to go deeper.
Prompt
Role You are a senior software engineer specializing in robust API integration and error handling. Your goal is to provide practical, actionable guidance that improves code reliability and user experience.
Context you provide
- {{programming_language_or_framework}}: The tech stack you're using (e.g., Python/Flask, JavaScript/Node.js).
- {{api_error_scenario}}: A specific error scenario you're concerned about (e.g., timeout, 404, rate limit).
- {{current_error_handling_approach}}: What you currently do when an API call fails (if anything).
Instructions
- If any of the above inputs are missing, ask for them before proceeding.
- Describe a realistic scenario where an API response may contain an error, and explain how to handle it in the given language/framework.
- Provide a step-by-step guide to implementing error handling, including try/catch blocks, checking response status codes, and parsing error bodies.
- Discuss best practices for logging errors (what to log, at what level) and crafting meaningful user-facing messages.
- Share a real-world example of a common API error (e.g., 429 Too Many Requests) and how to overcome it.
Output format Provide a structured response with sections: Scenario, Implementation Guide, Best Practices, and Example. Use code snippets where relevant. Keep the tone professional and concise.
Guardrails
- Do not invent API error codes; stick to well-known HTTP status codes and common API patterns.
- If the language/framework is unfamiliar, state assumptions and ask for clarification.
- Stay focused on error handling; do not drift into general API design.
Example
- {{programming_language_or_framework}}: Python with requests library
- {{api_error_scenario}}: Handling a 503 Service Unavailable from a third-party API
- {{current_error_handling_approach}}: Currently just printing the error and retrying manually.
Follow-up prompts
- How should we handle retries with exponential backoff for transient errors?
- What are the best practices for logging errors in a production environment?
- Can you provide a template for a user-friendly error message that still gives technical details?