返回值
¥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_ok
或 napi_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_api_types.h
中定义了整套可能的 napi_status
值。
¥The full set of possible napi_status
values is defined
in napi_api_types.h
.
napi_status
返回值提供了所发生错误的独立于 VM 的表示。在某些情况下,能够获得更详细的信息很有用,包括表示错误的字符串以及特定于 VM(引擎)的信息。
¥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
:所发生错误的文本表示。¥
error_message
: Textual representation of the error that occurred. -
engine_reserved
:不透明句柄仅供引擎使用。¥
engine_reserved
: Opaque handle reserved for engine use only. -
engine_error_code
:VM 特定的错误代码。¥
engine_error_code
: VM specific error code. -
error_code
:最后一个错误的节点 API 状态代码。¥
error_code
: Node-API status code for the last error.
napi_get_last_error_info
返回最后一次调用 Node-API 的信息。
¥napi_get_last_error_info
returns the information for the last
Node-API call that was made.
不要依赖任何扩展信息的内容或格式,因为它不受 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.