Claude Code prompt to add error handling
Error handling added ad hoc turns into try blocks around everything and information about nothing. Here Claude Code greps the repo for the actual gaps, ranks them by user impact, and proves each fix with a test that forces the failure, all in the same terminal session, so the diff you review maps to real risks.
Last updated
Audit this codebase for missing error handling, then fix it in order of blast radius. First produce the audit: grep for empty catch blocks, unhandled promise rejections, fetch or database calls without a failure path, and absent process-level handlers. Show me the list ranked by user impact before changing code. Then apply fixes: every external call gets a typed error or result wrapper, user-facing failures get a message that states what to retry, internal failures get structured logs with enough context to reproduce, and nothing swallows an error silently. Do not wrap everything in one giant try block. For each fix, add a test that forces the failure, network refused, bad JSON, constraint violation, and asserts the handled behavior. Run the suite and paste the failing-then-passing output for at least the three highest-impact fixes.
Same task in other tools
Questions about this prompt
Why produce an audit before fixing anything?
A repo-wide error-handling sweep with no map becomes a diff nobody can review. The ranked list lets you cut scope, fix the top of it now, and file the rest, and the agent can grep out the candidates in seconds.
Can I scope this to one service or directory?
Yes, name the path in the first line and the grep patterns run against that subtree only. The blast-radius ranking matters even more when scoped, since a payment path and a logging helper deserve different rigor.
The agent invented new error classes that duplicate ones we already have. Fix?
Point it at your existing error module before it starts, for example reuse the types in src/errors.ts. Given a named file, Claude Code extends what is there instead of forking a parallel hierarchy.