Continue Error Handling Prompt Template
Your code fails in predictable ways: it catches only the errors it can handle, gives clear messages, and lets the rest surface. This version is set up for Continue: use @ context providers to add files, the current selection or other context, so the model sees the code in question. Below you'll find the prompt, how to run it in Continue, a worked example and answers to common questions.
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 Context - Files involved: [list the files, or reference them in the chat] - How I build and test: [e.g. npm test]
How to use this prompt
- 1
Open Continue
Open the Continue chat panel in VS Code or JetBrains and paste the prompt.
- 2
Give it context
Use @ context providers to add files, the current selection or other context, so the model sees the code in question.
- 3
Review and iterate
Apply suggested edits after reading them and keep follow-ups in the same chat.
- 4
Save what you repeat
Continue is configurable, so you can save a prompt you reuse as a custom command or prompt file.
Working in Continue
- •Continue is open source and model-agnostic. Results depend on the model you connected, so test the prompt with the model you plan to use.
- •Select the code before you ask, so the answer is about that code.
- •Smaller local models do better with shorter prompts; trim optional sections if replies wander.
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.
- •Select the code before you ask, so the answer is about that code.
- •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 coding 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.
Does the prompt change with the model I use in Continue?
The wording stays the same, but quality varies by model. If a smaller model struggles, cut optional sections and ask for one step at a time.
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 coding prompts in one Markdown file, delivered by email. The templates here stay free either way — the pack just saves you the copying.