Complete AI Training

Prompt · Software Engineers

Robust API Error Handling

Use this when you need to design or improve error handling for API responses in your code.

All 22 prompts in this lesson

How to use it

  1. Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
  2. Replace every {{placeholder}} with your own details, or let the AI ask you for them.
  3. 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

  1. If any of the above inputs are missing, ask for them before proceeding.
  2. Describe a realistic scenario where an API response may contain an error, and explain how to handle it in the given language/framework.
  3. Provide a step-by-step guide to implementing error handling, including try/catch blocks, checking response status codes, and parsing error bodies.
  4. Discuss best practices for logging errors (what to log, at what level) and crafting meaningful user-facing messages.
  5. 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?