Errors¶
Every error of the daemon, of the library and of the framework derives from
SandboxError, with one class per kind
of failure and typed attributes, so that a program tells them apart without
reading messages; a misuse of the API raises a built-in exception instead (see
Outside the hierarchy). This page covers the
hierarchy, the attributes, which errors are worth a retry, and the errors of a
block whose cleanup fails.
import fastsandbox
from fastsandbox.exceptions import ConflictError, NotFoundError, SandboxError
async def main() -> None:
client = await fastsandbox.connect()
try:
await client.sandbox("docs-no-such-sandbox").inspect()
except NotFoundError as error:
print(f"not found: HTTP {error.status}, during {error.operation}")
async with client.create("docs-errors", kit="shell"):
try:
await client.create("docs-errors", kit="shell")
except ConflictError as error:
print(f"conflict: HTTP {error.status}, transient {error.transient}")
except SandboxError as error:
print(f"another error of fastsandbox: {error}")
if __name__ == "__main__":
fastsandbox.run(main())
The errors are in fastsandbox.exceptions; the
most used are also exported by fastsandbox itself: SandboxError,
ApiError, NotFoundError, ConflictError, DaemonUnavailableError,
SandboxTimeoutError, CommandFailedError, InvalidSpecError,
InvalidHostPatternError, LaunchError and TaskError.
The hierarchy¶
Every error has a message, the operation that failed when it is known
(sandbox start, sandbox inspection…), and transient. Some also derive
from the built-in exception that matches, so that generic handlers keep
working.
| Exception | Raised when | Attributes |
|---|---|---|
SandboxError |
the base of all | message, operation, transient |
DaemonUnavailableError (ConnectionError) |
the daemon cannot be reached: no socket, a refused or broken connection | path |
SandboxTimeoutError (TimeoutError) |
an operation got no outcome within its deadline | after |
ApiError |
the daemon answered with an error status | status, code, detail |
NotFoundError |
404: the sandbox, image, rule or execution does not exist | as ApiError |
ConflictError |
409: the request conflicts with the current state, a name in use or a stopped sandbox | as ApiError |
InvalidInputError |
400: the daemon rejected the request as invalid | as ApiError |
OperationFailedError |
the daemon reported a failure inside a successful answer: a rule change, an image pull, a creation (an error line, such as image is not cached locally with PullPolicy.NEVER) |
|
ProtocolError |
an answer of the daemon could not be decoded | |
UnsupportedVersionError |
the API version of the daemon is outside the supported range (see The API version) | found, supported |
InvalidRequestError (ValueError) |
a request was rejected before being sent: a host a network rule cannot name (see The hosts a rule names), or a proxied key without a host, for instance | |
InvalidHostPatternError (ValueError) |
HostPattern(text) refused a text that names no host a rule can name |
pattern, reason |
HostFileError (OSError) |
a file or directory of the host could not be accessed, the spec.yaml of a mixin directory among them |
path |
ArchiveError |
a tar archive could not be built or extracted | |
CommandFailedError |
a checked command exited with a non-zero code (see Commands) | exit_code, stdout, stderr |
KitPackageError |
a kit package directory is missing or invalid | path |
KitCompileError |
the sbx CLI could not compile a kit |
|
InvalidKitError |
the forge refuses a kit before anything is sent: one not named as sbx create names it (see Compositions), a mixin only the CLI resolves, a descriptor or an MCP server it cannot take, an argument the kit does not declare or a value it refuses |
kit, reason |
KitForgeError |
a kit could not be forged without the sbx CLI: a base kit only the CLI builds, a directory or a Git repository, or the kit policy of the daemon forbids it |
|
InvalidSpecError (ValueError) |
a sandbox spec or the values of its keys break a rule of a launch, which the message names with its field | |
LaunchError |
a launch failed once its sandbox existed; the sandbox was removed unless kept, and the error of the daemon is its __cause__ |
sandbox, id, step, kept |
TaskError |
a task of a FastSandbox application failed or is misdeclared |
NotFoundError, ConflictError and InvalidInputError derive from
ApiError; every other class derives from SandboxError directly. code is
the machine-readable code of the daemon, when it gives one, such as
desired_port_not_found.
Retrying¶
transient says whether the same call
may succeed later: it is set for a daemon that could not be reached, a
deadline that expired, an answer of the daemon with a 5xx status, and a
conflict, since the state the request conflicts with may change. A command
sent to a stopped sandbox succeeds once the sandbox runs; a name in use stays
in use until its sandbox is deleted, so a retry is the program's decision.
The daemon also answers a few invalid inputs with a server error, which a retry does not fix: an upload into a directory that does not exist, an image archive it cannot load.
Leaving a block whose cleanup fails¶
Four async with blocks clean up on the way out:
| Block | Its cleanup |
|---|---|
async with client.create(...) as sandbox |
the deletion of the sandbox |
async with spec.launch(...) as lease, or launch_package |
the removal of the lease and its sandbox |
async with stream, a Stream |
the closing of the stream |
async with await sandbox.ssh_tunnel() as tunnel |
the closing of the SSH tunnel |
The cleanup never masks the error of the block. When the block raised, an
error, a cancellation or Ctrl-C, and the cleanup fails too, the error of the
block goes on as it is, its type kept for the handlers around the block: the
failed cleanup is noted on it (__notes__, shown in its traceback), such as
the deletion of sandbox job-1 (id …) failed: ConflictError: …, and logged as
a warning on the fastsandbox logger. When the block ended well, the error of
the cleanup is raised, so that the program knows what may stay. A cleanup that
is itself cancelled is not caught.
The run of a task holds a lease the same way, with one difference: a removal that fails after a run that ended well is logged on the logger of the task, and the result is returned (see Running tasks).
Outside the hierarchy¶
A misuse of the API raises a built-in exception, not a SandboxError. A
stream, an interactive session, a tunnel or a lease used after it was closed,
shut down or detached raises a RuntimeError; an argument that is not one, a
duration, a mixin form or an MCP server description, a ValueError, as does
client.create() with neither a kit nor a request; an image a kit package
does not have (KitPackage.image_archive)
an IndexError.