PatternsHandle Errors Gracefully

Handle Errors Gracefully

Build an app that keeps working when a call is refused, unavailable or too fast.

A bridge call can fail, and most of the time that is normal: the person said no, the phone cannot do it, or an older Aksa does not know the method. An app that treats every failure as a crash feels broken. This guide builds a small notes app that answers each failure with a clear message and a fallback.

Read Error Codes first if you have not. This guide uses the five codes you will meet most: 4001, 4200, 4290, -32601 and the ok: false result of storage.set.

Step 1: The Manifest

manifest.json
{
  "manifestVersion": 1,
  "id": "progress-note",
  "name": "Progress Note",
  "version": "1.0.0",
  "author": "Your Name",
  "description": "Write a short note about today, save it, share it and check in.",
  "category": "productivity",
  "entry": "index.html",
  "icon": "icon.svg",
  "ageRating": 4,
  "url": "https://apps.aksaspace.com/app/progress-note",
  "bridge": {
    "version": 1,
    "scopes": ["actions.checkin"],
    "methods": ["actions.checkIn", "clipboard.write", "context.get", "share.open", "storage.get", "storage.set"]
  },
  "action": { "title": "Write today's note", "minMinutes": 1, "proof": "mini_app" },
  "data": { "network": "none" }
}

Step 2: One Function For Every Call

Wrap the bridge once. It never throws: it returns what happened, so the rest of the page reads straight through.

index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Progress Note</title>
<style>
  body { font-family: system-ui, sans-serif; margin: 0; padding: 24px; background: #fefefe; color: #111; }
  textarea { width: 100%; min-height: 96px; font: inherit; padding: 12px; border-radius: 12px; border: 1px solid #ddd; box-sizing: border-box; }
  button { font: inherit; padding: 10px 18px; border-radius: 999px; border: 1px solid #ddd; background: #111; color: #fff; margin: 8px 8px 0 0; }
  #status { margin-top: 12px; color: #555; min-height: 1.5em; }
</style>
</head>
<body>
<h1>Progress Note</h1>
<textarea id="note" placeholder="What did you do today?"></textarea>
<div>
  <button id="save">Save</button>
  <button id="share">Share</button>
  <button id="checkin">Check In</button>
</div>
<p id="status" role="status"></p>
<script>
  var NOTE_KEY = "note";

  // Never throws. Resolves { ok, data, code }.
  function bridge(method, params) {
    if (!window.aksa) return Promise.resolve({ ok: false, code: "no_host" });
    return window.aksa.request({ method: method, params: params || {} }).then(
      function (data) { return { ok: true, data: data }; },
      function (err) { return { ok: false, code: err && err.code }; }
    );
  }

  // One place that turns a failure into words for the person. Never show the raw message.
  function explain(what, code) {
    if (code === 4001) return "You chose not to allow " + what + ". You can change that in Settings, under Connected Apps.";
    if (code === 4200 || code === -32601 || code === "no_host") return what.charAt(0).toUpperCase() + what.slice(1) + " is not available here.";
    if (code === 4290) return "Too many taps. Wait a moment and try again.";
    return "Something went wrong with " + what + ". Try again.";
  }

  function say(text) { document.getElementById("status").textContent = text; }
  var noteEl = document.getElementById("note");

  bridge("storage.get", { key: NOTE_KEY }).then(function (r) {
    if (r.ok && r.data && r.data.value) noteEl.value = r.data.value;
  });

  document.getElementById("save").addEventListener("click", function () {
    bridge("storage.set", { key: NOTE_KEY, value: noteEl.value }).then(function (r) {
      if (!r.ok) return say(explain("saving", r.code));
      // A full quota is a normal result, not an error.
      if (r.data && r.data.ok === false) return say("Storage is full. Shorten the note and save again.");
      say("Saved.");
    });
  });

  document.getElementById("share").addEventListener("click", function () {
    var text = noteEl.value;
    bridge("share.open", { text: text }).then(function (r) {
      if (r.ok) return say("");
      // Fall back to the clipboard when the share sheet is not there.
      bridge("clipboard.write", { text: text }).then(function (c) {
        say(c.ok ? "Copied. Paste it where you like." : "Copy the note by hand.");
      });
    });
  });

  document.getElementById("checkin").addEventListener("click", function () {
    bridge("context.get").then(function (ctx) {
      var actionId = ctx.ok && ctx.data ? ctx.data.actionId : undefined;
      return bridge("actions.checkIn", { actionId: actionId, summary: noteEl.value.slice(0, 280) });
    }).then(function (r) {
      say(r.ok ? "Checked in." : explain("check-in", r.code));
    });
  });
</script>
</body>
</html>

Step 3: What To Notice

  • The wrapper never throws. Every handler reads r.ok instead of nesting try and catch.
  • ok: false is not an error. storage.set resolves normally when the storage is full, so check r.data.ok as well as r.ok.
  • One function words every failure. A refusal (4001) and "not available" (4200) read differently, because the person can fix one and not the other.
  • The fallback is part of the feature. When the share sheet is missing, the app copies the text instead of giving up.
  • A person who said no stays at no. The app tells them where to change it and never asks again in a loop. See Error Codes.
  • Outside Aksa the page still works. With no window.aksa the wrapper reports no_host and the messages say "not available here".

Step 4: Test The Failures

Failures are easy to skip because they are hard to cause. Cause them:

  • Open the page as a plain file. Every button should answer with a message, not do nothing.
  • In the simulator, watch what each call returns and check that every button leaves a message on the page.
  • On a phone, tap Save and Check In many times in a row to reach the rate limit (30 requests in 10 seconds) and check that the message is calm. See Limits And Quotas.

Next Steps

Build the habit side with Build A Daily Content App, and read Production Readiness before you submit.

Ready To Build Or Ship?

Check your files, preview on an iPhone frame, and submit in the Developer Studio.