Error handling
Deno Sandboxes provide a sandboxed environment for evaluating JavaScript code. This is useful for evaluating code that is not trusted or for testing code that is not safe to run in the main runtime.
Commands in a sandbox throw by default on non-zero exit. You can use the
noThrow() method to handle errors manually.
import { Sandbox } from "@deno/sandbox";
await using sandbox = await Sandbox.create();
// Commands throw by default on non-zero exit
try {
await sandbox.sh`exit 1`;
} catch (error) {
console.log("Command failed:", error);
}
// Use noThrow() to handle errors manually
const result = await sandbox.sh`exit 1`.noThrow();
console.log("Exit code:", result.status.code); // → 1
console.log("Success:", result.status.success); // → false
For more information about Sandboxes, see the Sandboxes documentation.