错误


🌐 Errors

在 Node.js 中运行的应用通常会遇到以下几类错误:

🌐 Applications running in Node.js will generally experience the following categories of errors:

  • 标准的 JavaScript 错误,例如 <EvalError><SyntaxError><RangeError><ReferenceError><TypeError><URIError>
  • 标准 DOMException
  • 由底层操作系统限制引发的系统错误,例如尝试打开不存在的文件或尝试通过已关闭的套接字发送数据。
  • AssertionError 是一种特殊类型的错误,当 Node.js 检测到不应该发生的异常逻辑违规时,会触发这种错误。这类错误通常由 node:assert 模块引发。
  • 由应用代码触发的用户指定错误。

由 Node.js 引发的所有 JavaScript 和系统错误都继承自标准 JavaScript <Error> 类,或者是该类的实例,并且保证至少提供该类上的属性。

🌐 All JavaScript and system errors raised by Node.js inherit from, or are instances of, the standard JavaScript <Error> class and are guaranteed to provide at least the properties available on that class.

Node.js 抛出的错误的 error.message 属性可能在任何版本中发生变化。请改用 error.code 来识别错误。对于 DOMException,使用 domException.name 来识别其类型。

🌐 The error.message property of errors raised by Node.js may be changed in any versions. Use error.code to identify an error instead. For a DOMException, use domException.name to identify its type.