错误行为


【Error behavior】

当策略检查失败时,Node.js 默认会抛出错误。可以通过在策略清单中定义 "onerror" 字段,将错误行为更改为几种可能的选项之一。可以使用以下值来更改行为:

【When a policy check fails, Node.js by default will throw an error. It is possible to change the error behavior to one of a few possibilities by defining an "onerror" field in a policy manifest. The following values are available to change the behavior:】

  • "exit":将立即退出进程。任何清理代码都不会被执行。
  • "log":将在出错地点记录错误。
  • "throw":将在失败发生的地方抛出一个 JS 错误。这是默认设置。
{
  "onerror": "log",
  "resources": {
    "./app/checked.js": {
      "integrity": "sha384-SggXRQHwCG8g+DktYYzxkXRIkTiEYWBHqev0xnpCxYlqMBufKZHAHQM3/boDaI/0"
    }
  }
}