Retext
BoltApp BuilderError Handling

Bolt 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 Bolt: say which framework and features the app has, and which files or screens the change affects. Below you'll find the prompt, how to run it in Bolt, a worked example and answers to common questions.

Installs as a snippet — type ;errhandle to paste this prompt anywhere on your Mac.
Prompt Template
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
- App and screens involved: [framework, page or component names]
- Leave unchanged: [features, routes or styling that must not change]

How to use this prompt

  1. 1

    Open Bolt

    Paste the prompt into Bolt's chat to start or update a project.

  2. 2

    Give it context

    Say which framework and features the app has, and which files or screens the change affects.

  3. 3

    Review and iterate

    Check the live preview after each step and describe what is wrong in plain terms, one problem at a time.

Working in Bolt

  • •Bolt builds and runs the whole app in the browser, so a vague prompt can change more than you intended. Scope each request.
  • •Ask it to keep existing features working and to list the files it changed.
  • •For errors, paste the exact message from the preview or console instead of describing it.

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.
  • •Bolt builds and runs the whole app in the browser, so a vague prompt can change more than you intended. Scope each request.
  • •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.

  1. Click Add to Retext. The prompt installs as a snippet with the trigger ;errhandle.
  2. Change the trigger in Retext if you prefer a different one.
  3. 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 Bolt from rewriting the whole app?

Name the screen or file to change and add "Leave everything else as it is." Smaller requests give smaller, easier to check diffs.

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.