Improve error handling in your code

Review code for missing or weak error handling and get fixes for retries, clear messages, logging and failure paths, with tests for each case.

Published
Works with
ChatGPT, Claude and Gemini
Level
Intermediate
By
GoodAIPromt editorial team

Most bugs that reach users show up in the failure paths: a network call times out, a file is missing, input is empty, a third-party API returns something unexpected. Code often handles the happy path well and swallows or ignores the rest. This prompt reviews a piece of code specifically for error handling, lists each failure point and returns fixed code with clear messages, sensible retries and tests for the error cases.

The prompt

Act as a senior engineer reviewing error handling.

Language and framework: 
What this code does and who calls it: 
How errors should surface: 
Code:


1. List every place this code can fail (I/O, network, parsing, nulls, invalid input, timeouts, external APIs), with what happens today.
2. Flag anti-patterns: swallowed exceptions, catching everything, losing the original error, unclear messages, leaking sensitive data in errors, missing cleanup.
3. Rewrite the code with: input validation, specific error types, helpful messages, retries with backoff only where safe (idempotent operations), timeouts, cleanup, and structured logging with context but no secrets.
4. Explain each change in one line.
5. Write unit tests for the main failure cases.

Keep the public behaviour and function signatures the same unless a change is necessary; if so, say why.

0 of 4 blanks filled

How to use it

  1. Paste one module or function at a time with a sentence about how it's used.
  2. State how errors should reach callers so the fixes fit your conventions.
  3. Run the new tests and review retry logic carefully before merging.

Tips

  • Don't paste API keys, passwords or customer data into the prompt.
  • Retries on non-idempotent actions (like payments) can cause duplicates; check those by hand.

Frequently asked questions

What does this prompt do?

Review code for missing or weak error handling and get fixes for retries, clear messages, logging and failure paths, with tests for each case.

Which AI tools does it work with?

It is written for ChatGPT, Claude and Gemini. It also works in other AI tools that accept text prompts, with small changes.

What do I need to fill in?

4 blanks: e.g. python 3.12 / node.js / go; context; e.g. http status codes, exceptions to caller, user messages, logs; paste code. Click each blue blank on the page, type your details and press Copy prompt.

Is this prompt free to use?

Yes. You can copy and use it for personal and commercial work.

error handlingcode qualityexceptionstesting

More prompts for ChatGPT, Claude, Gemini.