Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ad793ab

Browse filesBrowse files
Gabriel SchulhofMylesBorins
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 <michael_dawson@ca.ibm.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent da8bc6a commit ad793ab
Copy full SHA for ad793ab

File tree

Expand file treeCollapse file tree

3 files changed

+23
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+23
-2
lines changed
Open diff view settings
Collapse file

‎doc/api/n-api.md‎

Copy file name to clipboardExpand all lines: doc/api/n-api.md
+2-2Lines changed: 2 additions & 2 deletions
  • Display the source diff
  • Display the rich diff
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
Collapse file

‎test/addons-napi/test_error/test.js‎

Copy file name to clipboardExpand all lines: test/addons-napi/test_error/test.js
+12Lines changed: 12 additions & 0 deletions
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
{
Collapse file

‎test/addons-napi/test_error/test_error.c‎

Copy file name to clipboardExpand all lines: test/addons-napi/test_error/test_error.c
+9Lines changed: 9 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.