Skip to content

Commit ad793ab

Browse files
Gabriel SchulhofMylesBorins
Gabriel Schulhof
authored andcommitted
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: nodejs/abi-stable-node#309 PR-URL: #20428 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent da8bc6a commit ad793ab

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

doc/api/n-api.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,11 @@ added: v8.0.0
344344
NODE_EXTERN napi_status napi_throw(napi_env env, napi_value error);
345345
```
346346
- `[in] env`: The environment that the API is invoked under.
347-
- `[in] error`: The `napi_value` for the `Error` to be thrown.
347+
- `[in] error`: The JavaScript value to be thrown.
348348

349349
Returns `napi_ok` if the API succeeded.
350350

351-
This API throws the JavaScript `Error` provided.
351+
This API throws the JavaScript value provided.
352352

353353
#### napi_throw_error
354354
<!-- YAML

test/addons-napi/test_error/test.js

+12
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ assert.throws(() => {
6060
test_error.throwTypeError();
6161
}, /^TypeError: type error$/);
6262

63+
function testThrowArbitrary(value) {
64+
assert.throws(() => {
65+
test_error.throwArbitrary(value);
66+
}, value);
67+
}
68+
69+
testThrowArbitrary(42);
70+
testThrowArbitrary({});
71+
testThrowArbitrary([]);
72+
testThrowArbitrary(Symbol('xyzzy'));
73+
testThrowArbitrary(true);
74+
6375
common.expectsError(
6476
() => test_error.throwErrorCode(),
6577
{

test/addons-napi/test_error/test_error.c

+9
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ napi_value createTypeErrorCode(napi_env env, napi_callback_info info) {
127127
return result;
128128
}
129129

130+
static napi_value throwArbitrary(napi_env env, napi_callback_info info) {
131+
napi_value arbitrary;
132+
size_t argc = 1;
133+
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, &arbitrary, NULL, NULL));
134+
NAPI_CALL(env, napi_throw(env, arbitrary));
135+
return NULL;
136+
}
137+
130138
napi_value Init(napi_env env, napi_value exports) {
131139
napi_property_descriptor descriptors[] = {
132140
DECLARE_NAPI_PROPERTY("checkError", checkError),
@@ -137,6 +145,7 @@ napi_value Init(napi_env env, napi_value exports) {
137145
DECLARE_NAPI_PROPERTY("throwErrorCode", throwErrorCode),
138146
DECLARE_NAPI_PROPERTY("throwRangeErrorCode", throwRangeErrorCode),
139147
DECLARE_NAPI_PROPERTY("throwTypeErrorCode", throwTypeErrorCode),
148+
DECLARE_NAPI_PROPERTY("throwArbitrary", throwArbitrary),
140149
DECLARE_NAPI_PROPERTY("createError", createError),
141150
DECLARE_NAPI_PROPERTY("createRangeError", createRangeError),
142151
DECLARE_NAPI_PROPERTY("createTypeError", createTypeError),

0 commit comments

Comments
 (0)