Skip to content

commandCode quality

Add Error Handling

Adds real error handling to one function: lists its failure modes, handles each with a clear message and cleans up resources.

npx workspacetemplates add add-error-handling --tool claude-code

Writes 1 file to .claude/commands/

.claude/commands/add-error-handling.md15 lines · 1.1 KBFile
---
description: "Adds real error handling to one function: lists its failure modes, handles each with a clear message and cleans up resources."
argument-hint: "[function name or file:line]"
---

Add error handling to: $ARGUMENTS

1. Locate the function and read it, its callers and its tests. Note how this codebase reports errors already (exceptions, Result types, error codes, HTTP responses) and copy that style.
2. List every failure mode before editing: invalid input, null or missing data, I/O and network errors, timeouts, partial writes, permission problems, and errors thrown by each dependency.
3. For each one decide: recover, retry with a limit, translate into a domain error, or let it propagate. Say which and why.
4. Write messages that name the operation and the offending value, never secrets or full payloads.
5. Make cleanup unconditional (finally, defer, using, context managers).
6. Never add an empty catch or a blanket catch that hides bugs. Do not change the function's successful behavior.

Add a test for each failure mode that can be triggered. Show the failure table and the diff, run the tests, then ask before changing any caller's signature.

What it does

Most functions only work on the happy path. This command takes one function, reads what it calls, and writes down every way it can fail before touching a line.

Each failure gets a specific response in the style the codebase already uses, whether that is typed errors, result values or exceptions, with cleanup that still runs when things go wrong.

Example

you › /add-error-handling uploadAvatar in src/users/avatar.ts

agent › Failure modes: file over 5 MB, wrong MIME type, storage timeout, DB update fails after upload.
Added AvatarError with 4 codes, retry once on timeout, deletes the orphan file if the DB write fails.
4 new tests pass. Callers unchanged. Update the route to map codes to HTTP statuses?

Questions

Will it wrap everything in try and catch?
No. It decides per failure whether to recover, translate or let it pass up, and refuses empty catches.
Does it change what callers see?
Only if you agree. Changes to a signature or thrown type are proposed and wait for your answer.
  • Add Logging

    CommandCode quality

    Free
  • Add Types

    CommandCode quality

    Free
  • Extract Function

    CommandCode quality

    Free
  • Find Dead Code

    CommandCode quality

    Free