Errors
Every bridge failure carries code and message, plus requires on a not-implemented. Branch on the code: most failures are rejected invokes, but window, tray, updater and others answer a typed { ok: false, code, message } value inside the { ok: true, result } envelope. The bridge gives that value a non-200 HTTP status mapped from the same table below, so the status agrees with the code instead of being informational.
Codes
| code | HTTP | cause |
|---|---|---|
unauthorized | 401 | missing or wrong bearer token |
bad-origin | 403 | present and foreign Origin header |
not-found | 404 | unknown asset route, or fs.readTextFile on a missing file |
unknown-plugin | 404 | PluginRegistry.invoke only — the bridge flattens names into unknown-command |
unknown-command | 404 | no handler under <plugin>.<command>, including an unknown plugin over the bridge |
bad-json | 400 | the invoke body is not JSON |
bad-request | 400 | malformed payload, bad argument, or a definePlugin violation |
handler-error | 500 | a handler threw something that was not a CrustError |
too-large | 413 | body over 2 MiB |
not-implemented | 501 | a command returned notImplemented(...) |
forbidden | 403 | a window label does not hold a capability covering the command |
outside-scope | 403 | an fs path resolves outside every configured fs.scope prefix, or touches the scope store |
Messages
| code | message | when |
|---|---|---|
unauthorized | missing or bad bearer token | the header is absent or wrong |
unauthorized | missing or bad document token | the document route's ?token= is absent or was never issued |
bad-json | body is not json | JSON.parse failed on the body |
too-large | payload too large | the declared or actual body exceeded 2 MiB |
bad-request | body needs { plugin, command, args } | the JSON body has no string plugin and command |
bad-request | binary invoke needs ?plugin= and ?command= | a non-JSON body with the command missing from the query string |
unknown-command | the bare key, e.g. fs.nope | no handler is registered at that key |
unknown-plugin | no plugin named "fs" | registry lookup only, never over the bridge |
not-implemented | the first argument of notImplemented(...), with requires set to the second | a command declined instead of pretending |
handler-error | <plugin>.<command> plugin handler threw: <message> (the error came from the handler, not the bridge) | a handler threw; its code, status and requires are kept |
forbidden | the action, the window label, and the capability that would have allowed it | the label holds no capability covering the command |
outside-scope | fs: <path> is outside the app's scope | the path resolves outside every prefix; the store and a page's own grant widening name the fix instead |
not-implemented reaches the page as { "code": "not-implemented", "message": "…", "requires": "NSPasteboard" }, so a caller can tell "this platform cannot do it" from "the call was wrong". A typed value's own code travels the same way: needs-activation from accessibility.elementCount is the read telling the caller the app was never activated, not a platform gap.
A definePlugin violation is bad-request at declaration time, not at invoke time: definePlugin: name must be a non-empty string, definePlugin: name "a.b" may not contain a dot, definePlugin: plugin "x" needs a commands object, definePlugin: plugin "x" has no commands, definePlugin: x.a.b may not contain a dot, definePlugin: x.y is not a function.
One sheet at a time
The shim holds one panel, one done flag and one path list, so a second openPanel or savePanel while a sheet is up answers SHIM_PANEL_BUSY (status 5) before touching anything. The adapter turns that into a thrown message, which the bridge reports as handler-error — HTTP 500 — naming the panel that is already up and the way out:
crust: savePanel refused (busy); an NSOpenPanel sheet is already up, and only one panel can be open at a time — answer it, or call dialog.cancel, before asking NSSavePanel for another
openPanel uses NSOpenPanel and savePanel uses NSSavePanel; the busy refusal names whichever class is already up. A cancelled pick is never an error: the panel answers { paths: [] }, and dialog.cancel answers { cancelled: true, paths: [] }, or { cancelled: false, paths: [] } when nothing was pending — an ordinary race with the user.
window and tray
These two return a typed failure value rather than a rejected invoke: { ok: false, code, message }, with label on every window failure that knows one. The bridge maps the code to a non-200 status through the CrustError table — invalid-* → 400, unknown-window and destroyed → 404, unknown-adapter → 501, handler-error → 500, server-side faults (signature-mismatch, hash-mismatch, unverified-artifact, relaunch-failed, io-error, corrupt, sql-error, contract-violation) → 500, any other unmapped code → 400 — while the { ok: true, result: … } envelope and body stay unchanged. Because the body is unchanged, await window.close() still resolves with the typed failure instead of rejecting, so a caller that ignores the result keeps working.
| code | raised when |
|---|---|
invalid-label | the command needs { label } with a non-empty string |
invalid-title | create or setTitle got a title that is not a string |
unknown-window | no open window carries that label |
duplicate-label | create names a label that is already open |
invalid-size | create width or height is not a positive integer — refused, never clamped |
invalid-url | create url is present but empty or not a string |
unknown-adapter | the adapter has no create, checked before the arguments |
handler-error | the adapter threw, or answered without tracking the new label |
| code | message |
|---|---|
invalid-label | crust: window command needs { label } with a non-empty string, got … |
invalid-title | crust: window.setTitle title must be a string, got 42 |
unknown-window | crust: no window labelled "main" |
duplicate-label | crust: a window labelled "main" already exists; a second one would replace it in the platform's map. Close it or pick another label. |
invalid-size | crust: window.create width must be a positive integer, got 1.5 in … |
invalid-url | crust: window.create url must be a non-empty string when given, got "" in … |
Tray codes are invalid-text, invalid-menu, unknown-adapter, destroyed and handler-error. destroyed means the status item was already torn down; a second createStatusBar() throws until the first is destroyed.