diff options
author | Gabriel Schulhof <gabriel.schulhof@intel.com> | 2018-04-29 23:49:30 +0300 |
---|---|---|
committer | Gabriel Schulhof <gabriel.schulhof@intel.com> | 2018-05-02 16:23:38 +0300 |
commit | bdf5be98dd901f6c312938198439dbda0b20d517 (patch) | |
tree | 3be2540235dd45e6c179ed8d0e315d0f96e70abc | |
parent | 9c8b479467a5f2b68f967444fbc2f7528082ab23 (diff) |
n-api: test and doc napi_throw() of a primitive
Ensure that napi_throw() is able to throw a primitive value, and
document that it is able to throw any JavaScript value.
Fixes: https://github.com/nodejs/abi-stable-node/issues/309
PR-URL: https://github.com/nodejs/node/pull/20428
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
-rw-r--r-- | doc/api/n-api.md | 4 | ||||
-rw-r--r-- | test/addons-napi/test_error/test.js | 12 | ||||
-rw-r--r-- | test/addons-napi/test_error/test_error.c | 9 |
3 files changed, 23 insertions, 2 deletions
diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 85993639dc8..fcbd4decb35 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -344,11 +344,11 @@ added: v8.0.0 NODE_EXTERN napi_status napi_throw(napi_env env, napi_value error); ``` - `[in] env`: The environment that the API is invoked under. -- `[in] error`: The `napi_value` for the `Error` to be thrown. +- `[in] error`: The JavaScript value to be thrown. Returns `napi_ok` if the API succeeded. -This API throws the JavaScript `Error` provided. +This API throws the JavaScript value provided. #### napi_throw_error <!-- YAML diff --git a/test/addons-napi/test_error/test.js b/test/addons-napi/test_error/test.js index d5c92cb4c3c..f07326c202a 100644 --- a/test/addons-napi/test_error/test.js +++ b/test/addons-napi/test_error/test.js @@ -60,6 +60,18 @@ assert.throws(() => { test_error.throwTypeError(); }, /^TypeError: type error$/); +function testThrowArbitrary(value) { + assert.throws(() => { + test_error.throwArbitrary(value); + }, value); +} + +testThrowArbitrary(42); +testThrowArbitrary({}); +testThrowArbitrary([]); +testThrowArbitrary(Symbol('xyzzy')); +testThrowArbitrary(true); + common.expectsError( () => test_error.throwErrorCode(), { diff --git a/test/addons-napi/test_error/test_error.c b/test/addons-napi/test_error/test_error.c index 1cad38098c9..c9ac7a425fa 100644 --- a/test/addons-napi/test_error/test_error.c +++ b/test/addons-napi/test_error/test_error.c @@ -127,6 +127,14 @@ napi_value createTypeErrorCode(napi_env env, napi_callback_info info) { return result; } +static napi_value throwArbitrary(napi_env env, napi_callback_info info) { + napi_value arbitrary; + size_t argc = 1; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &arbitrary, NULL, NULL)); + NAPI_CALL(env, napi_throw(env, arbitrary)); + return NULL; +} + napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor descriptors[] = { DECLARE_NAPI_PROPERTY("checkError", checkError), @@ -137,6 +145,7 @@ napi_value Init(napi_env env, napi_value exports) { DECLARE_NAPI_PROPERTY("throwErrorCode", throwErrorCode), DECLARE_NAPI_PROPERTY("throwRangeErrorCode", throwRangeErrorCode), DECLARE_NAPI_PROPERTY("throwTypeErrorCode", throwTypeErrorCode), + DECLARE_NAPI_PROPERTY("throwArbitrary", throwArbitrary), DECLARE_NAPI_PROPERTY("createError", createError), DECLARE_NAPI_PROPERTY("createRangeError", createRangeError), DECLARE_NAPI_PROPERTY("createTypeError", createTypeError), |