返回值


【Return values】

所有的 Node-API 函数都遵循相同的错误处理模式。所有 API 函数的返回类型都是 napi_status

【All of the Node-API functions share the same error handling pattern. The return type of all API functions is napi_status.】

如果请求成功且未抛出未捕获的 JavaScript 异常,返回值将是 napi_ok。如果发生错误并且抛出了异常,将返回表示该错误的 napi_status 值。如果抛出了异常但没有发生错误,将返回 napi_pending_exception

【The return value will be napi_ok if the request was successful and no uncaught JavaScript exception was thrown. If an error occurred AND an exception was thrown, the napi_status value for the error will be returned. If an exception was thrown, and no error occurred, napi_pending_exception will be returned.】

在返回值不是 napi_oknapi_pending_exception 的情况下,必须调用 napi_is_exception_pending 来检查是否有待处理的异常。有关详细信息,请参阅异常部分。

【In cases where a return value other than napi_ok or napi_pending_exception is returned, napi_is_exception_pending must be called to check if an exception is pending. See the section on exceptions for more details.】

napi_status 的所有可能值的完整集合定义在 napi_api_types.h 中。

【The full set of possible napi_status values is defined in napi_api_types.h.】

napi_status 返回值提供了一个与虚拟机无关的错误表示。在某些情况下,获取更详细的信息是有用的,包括表示错误的字符串以及与虚拟机(引擎)相关的特定信息。

【The napi_status return value provides a VM-independent representation of the error which occurred. In some cases it is useful to be able to get more detailed information, including a string representing the error as well as VM (engine)-specific information.】

为了检索此信息,提供了 napi_get_last_error_info,它会返回一个 napi_extended_error_info 结构体。napi_extended_error_info 结构体的格式如下:

【In order to retrieve this information napi_get_last_error_info is provided which returns a napi_extended_error_info structure. The format of the napi_extended_error_info structure is as follows:】

typedef struct napi_extended_error_info {
  const char* error_message;
  void* engine_reserved;
  uint32_t engine_error_code;
  napi_status error_code;
}; 
  • error_message:发生错误的文本描述。
  • engine_reserved:仅供引擎使用的不透明句柄。
  • engine_error_code:虚拟机特定的错误代码。
  • error_code:上一个错误的 Node-API 状态码。

napi_get_last_error_info 返回上一次 Node-API 调用的信息。

不要依赖任何扩展信息的内容或格式,因为它不受语义化版本控制(SemVer)约束,可能随时发生变化。它仅用于记录目的。

【Do not rely on the content or format of any of the extended information as it is not subject to SemVer and may change at any time. It is intended only for logging purposes.】