Skip to main content

错误处理

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.

沙箱中的命令在非零退出时默认会抛出错误。你可以使用
noThrow() 方法来手动处理错误。

import { Sandbox } from "@deno/sandbox";

await using sandbox = await Sandbox.create();

// 命令在非零退出时默认抛出错误
try {
  await sandbox.sh`exit 1`;
} catch (error) {
  console.log("命令失败:", error);
}

// 使用 noThrow() 手动处理错误
const result = await sandbox.sh`exit 1`.noThrow();
console.log("退出码:", result.status.code); // → 1
console.log("成功:", result.status.success); // → false

For more information about Sandboxes, see the Sandboxes documentation.

你找到了你需要的东西吗?

编辑此页面
隐私政策