From df574f3109cd8390eb526eb78e0612d769792f50 Mon Sep 17 00:00:00 2001 From: Idan Attias Date: Sat, 6 Nov 2021 10:36:38 +0200 Subject: [PATCH] src,doc: add SyntaxError napi support Add `napi_create_syntax_error` and `napi_throw_syntax_error`. Fixes: https://github.com/nodejs/node-addon-api/issues/1099 PR-URL: https://github.com/nodejs/node/pull/40736 Reviewed-By: James M Snell Reviewed-By: Michael Dawson --- doc/api/n-api.md | 52 ++++++++++++++++++++-- src/js_native_api.h | 11 +++++ src/js_native_api_v8.cc | 38 ++++++++++++++++ test/js-native-api/test_error/test.js | 23 ++++++++++ test/js-native-api/test_error/test_error.c | 37 +++++++++++++++ 5 files changed, 157 insertions(+), 4 deletions(-) diff --git a/doc/api/n-api.md b/doc/api/n-api.md index 912a3769b4cb9e..883efc545b9b51 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -1065,12 +1065,11 @@ JavaScript value to be thrown. The following utility functions are also available in case native code needs to throw an exception or determine if a `napi_value` is an instance of a JavaScript `Error` object: [`napi_throw_error`][], -[`napi_throw_type_error`][], [`napi_throw_range_error`][] and -[`napi_is_error`][]. +[`napi_throw_type_error`][], [`napi_throw_range_error`][], [`node_api_throw_syntax_error`][] and [`napi_is_error`][]. The following utility functions are also available in case native code needs to create an `Error` object: [`napi_create_error`][], -[`napi_create_type_error`][], and [`napi_create_range_error`][], +[`napi_create_type_error`][], [`napi_create_range_error`][] and [`node_api_create_syntax_error`][], where result is the `napi_value` that refers to the newly created JavaScript `Error` object. @@ -1180,6 +1179,25 @@ Returns `napi_ok` if the API succeeded. This API throws a JavaScript `RangeError` with the text provided. +#### `node_api_throw_syntax_error` + + + +````c +NAPI_EXTERN napi_status node_api_throw_syntax_error(napi_env env, + const char* code, + const char* msg); + +* `[in] env`: The environment that the API is invoked under. +* `[in] code`: Optional error code to be set on the error. +* `[in] msg`: C string representing the text to be associated with the error. + +Returns `napi_ok` if the API succeeded. + +This API throws a JavaScript `SyntaxError` with the text provided. + #### `napi_is_error` + +```c +NAPI_EXTERN napi_status node_api_create_syntax_error(napi_env env, + napi_value code, + napi_value msg, + napi_value* result); +``` + +* `[in] env`: The environment that the API is invoked under. +* `[in] code`: Optional `napi_value` with the string for the error code to be + associated with the error. +* `[in] msg`: `napi_value` that references a JavaScript `string` to be used as + the message for the `Error`. +* `[out] result`: `napi_value` representing the error created. + +Returns `napi_ok` if the API succeeded. + +This API returns a JavaScript `SyntaxError` with the text provided. + #### `napi_get_and_clear_last_exception`