Error enum, TypeScript exposes a dedicated subclass per variant (use instanceof), Python provides dedicated exception classes, and Go provides an *Error value with an ErrorKind discriminator matched via m.IsKind(err, kind) or errors.As. Ruby currently exposes Microsandbox::Error; where no dedicated subclass exists, its reference documents the stable message contract explicitly.
Matching errors
Spawn-time exec failures
exec() distinguishes between:
- A program that ran and exited non-zero: the call returns an
ExecOutputwith a non-zerocode. This is not an error in the SDK sense; it’s a normal result. - A program that never started: the binary doesn’t exist, isn’t executable, the working directory is unreachable, etc. This surfaces as a typed error variant:
ExecFailed(Rust),ExecFailedError(TypeScript),ExecFailedError(Python).
kind plus the underlying errno, so callers can branch on the cause and react. Common kinds: NotFound (binary missing on PATH), PermissionDenied, NotExecutable, BadCwd, BadArgs, ResourceLimit, UserSetupFailed, OutOfMemory, PtySetupFailed, Other.
127 for NotFound, 126 for PermissionDenied and NotExecutable, 1 otherwise. SDK callers reading the error directly don’t need to think about exit codes; branch on kind instead.
Name conflicts
Creating a sandbox with a name that’s already in use (and withoutreplace) surfaces a typed error you can branch on to decide whether to recover (resume the existing one, regenerate the name, etc.).
connect_or_create and its language-idiomatic equivalents to converge on the existing persisted identity without changing its configuration. Pass replace() / replace=True / replace: true / --replace / WithReplace() only when you intend to stop the existing sandbox and create a new identity. See Naming conflicts for the grace-period knob.
Stale receiver identities
Built-in local and cloud lifecycle receivers capture a stable sandbox identity in addition to the reusable name. If the name now points to a replacement, receiver lifecycle operations refuse to act on it and surface a typed stale-identity error.Microsandbox::Error with the stable was replaced message is the Ruby-specific contract; the operation still refuses to act on the replacement.
Sandbox start failures
When a sandbox process exits before the agent relay is ready (mount errors, missing rootfs, network setup failures), the SDK surfaces a typedBootStart / BootStartError. The payload carries the failure stage and errno so callers can recover or report cleanly.
error: block before any captured log output, so you see “what went wrong + a hint” inline. SDK callers get the structured payload to make their own decisions.
Resource cleanup
Sandboxes hold compute resources, so release them when done. In Rust,Drop handles cleanup when the sandbox goes out of scope. In TypeScript, prefer await using (Node 22+) which calls Sandbox.stop() automatically when the binding leaves scope. In Go, pair every CreateSandbox with a defer that calls Stop + Close.