使用自定义错误类进行错误处理
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.
你可以在沙盒中使用自定义错误类来处理错误。
import { Sandbox, SandboxCommandError } from "@deno/sandbox";
await using sandbox = await Sandbox.create();
try {
await sandbox.sh`exit 42`;
} catch (error) {
if (error instanceof SandboxCommandError) {
console.log("退出代码:", error.code); // → 42
console.log("错误信息:", error.message);
}
}
For more information about Sandboxes, see the Sandboxes documentation.