Error Handling Prompt Templates
Your code fails in predictable ways: it catches only the errors it can handle, gives clear messages, and lets the rest surface. The prompt below works in any AI assistant. It comes with a worked example, the mistakes that make AI output for this task weak, and variations for common situations.
Add proper error handling to the code below. ## Code Language and runtime: [e.g. Node 20] [paste the code] ## How it is used Called by: [e.g. a CLI at startup, an HTTP handler, a background job] What the caller should see when it fails: [e.g. a clear message and exit code 1, a 400 response] ## Failure cases I know about [e.g. file missing, invalid JSON, network timeout] ## Instructions - Catch only errors you can handle or add context to. Re-throw everything else. - Make error messages say what failed and what input caused it. - Use a custom error type if the caller needs to tell failures apart. - Validate inputs at the boundary instead of deep inside. - Do not swallow errors or log and continue silently. ## Output format 1. Updated code 2. A table of failure case, what happens, and what the caller sees 3. Any failure case I did not list that you think matters
How to use this prompt
- 1
Copy the prompt
Use the copy button, or type its Retext trigger in any app to paste it.
- 2
Open your AI assistant
Start a new chat in ChatGPT, Claude, Gemini or whichever assistant you use, so earlier messages don't steer the answer.
- 3
Fill in the brackets
Replace every [bracketed] part with your real details. Specific inputs are what make the answer specific.
- 4
Check and refine
Read the result against the goal above, then reply with one change at a time.
Worked example
What you give it
Language: Node 20 (CommonJS)
Called by: a CLI at startup.
Caller should see: a clear message about the config file.
Known failures: file missing, invalid JSON after someone edits it by hand.
const fs = require("fs");
function loadConfig(path) {
return JSON.parse(fs.readFileSync(path, "utf8"));
}What a good answer looks like (shortened)
1. Updated code:
const fs = require("fs");
class ConfigError extends Error {}
function loadConfig(path) {
let raw;
try {
raw = fs.readFileSync(path, "utf8");
} catch (err) {
if (err.code === "ENOENT") {
throw new ConfigError("Config file not found: " + path);
}
throw err;
}
try {
return JSON.parse(raw);
} catch (err) {
throw new ConfigError("Config file is not valid JSON: " + path + " (" + err.message + ")");
}
}
2. Missing file: ConfigError with the path. Invalid JSON: ConfigError with the parse message. Permission denied: original error re-thrown.
3. Not listed: an empty file also fails JSON parsing and gets the invalid JSON message, which is acceptable.Tips for better results
- •Fill in "What the caller should see." A CLI, an API handler and a background job need very different behavior from the same failure.
- •Check the table in item 2 for any row where the caller sees nothing. Silent failures are the ones worth pushing back on.
- •Use item 3 as a checklist. The AI often spots cases like permission errors or empty input that you did not list.
- •Avoid this: Asking for "better error handling" without saying who the caller is, which leads to try/catch blocks everywhere that log and continue.
Get the free prompt pack
Our best AI prompts in one Markdown file, delivered by email. The templates here stay free either way — the pack just saves you the copying.
Variations
HTTP API
Replace How it is used with: An Express handler. Map validation errors to 400, not found to 404, and everything else to 500 without leaking internals.
Retries
Add this line to Instructions: For network calls, retry transient errors up to 3 times with backoff and fail fast on 4xx responses.
Typed results
Add this line to Instructions: Return a result type like { ok: true, value } or { ok: false, error } instead of throwing.Save it as a Retext snippet
Retext is a free text expander for Mac. Save this prompt once, then type ;errhandle in any app (ChatGPT, Claude, your editor, a browser tab) and the full prompt is pasted in.
- Click Add to Retext. The prompt installs as a snippet with the trigger ;errhandle.
- Change the trigger in Retext if you prefer a different one.
- Type the trigger wherever you want the prompt, then fill in the brackets.
Frequently asked questions
Should every function have a try/catch?
No. Catch errors where you can do something useful, like add context, retry, or return a clear response. Elsewhere, let them propagate to a single top-level handler.
How do I keep AI from swallowing errors?
Say it explicitly in the prompt, as the Instructions section does, and check the output for empty catch blocks or catches that only log.
How do I reuse this prompt without copying it every time?
Click Add to Retext to save it as a snippet. Then type ;errhandle in any Mac app, including your AI tool, and the full prompt is pasted in.
Related Templates
Get the free prompt pack
Our best AI prompts in one Markdown file, delivered by email. The templates here stay free either way — the pack just saves you the copying.