1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(napi): functions related to errors (#17370)

This commits fixes various NAPI functions related to creation and
throwing of errors.
This commit is contained in:
Bartek Iwańczuk 2023-01-15 04:36:55 +01:00 committed by GitHub
parent e4c6e6e95f
commit df8bfa26be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 1071 additions and 305 deletions

View file

@ -158,8 +158,8 @@ fn napi_get_node_version(
env: *mut Env,
result: *mut *const napi_node_version,
) -> Result {
let _: &mut Env = env.as_mut().ok_or(Error::InvalidArg)?;
crate::check_arg!(result);
crate::check_env!(env);
crate::check_arg!(env, result);
*result = &NODE_VERSION as *const napi_node_version;
Ok(())

File diff suppressed because it is too large Load diff

View file

@ -311,7 +311,7 @@ pub struct napi_extended_error_info {
pub error_message: *const c_char,
pub engine_reserved: *mut c_void,
pub engine_error_code: i32,
pub status_code: napi_status,
pub error_code: napi_status,
}
#[repr(C)]
@ -416,6 +416,7 @@ pub struct Env {
pub cleanup_hooks:
Rc<RefCell<Vec<(extern "C" fn(*const c_void), *const c_void)>>>,
pub tsfn_ref_counters: Rc<RefCell<ThreadsafeFunctionRefCounters>>,
pub last_error: napi_extended_error_info,
}
unsafe impl Send for Env {}
@ -450,6 +451,12 @@ impl Env {
threadsafe_function_sender,
cleanup_hooks,
tsfn_ref_counters,
last_error: napi_extended_error_info {
error_message: std::ptr::null(),
engine_reserved: std::ptr::null_mut(),
engine_error_code: 0,
error_code: napi_ok,
},
}
}

View file

@ -4,6 +4,7 @@ export {
assert,
assertEquals,
assertRejects,
assertThrows,
} from "../test_util/std/testing/asserts.ts";
export { fromFileUrl } from "../test_util/std/path/mod.ts";

215
test_napi/error_test.js Normal file
View file

@ -0,0 +1,215 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import {
assert,
assertEquals,
assertThrows,
loadTestLibrary,
} from "./common.js";
const testError = loadTestLibrary();
const theError = new Error("Some error");
const theTypeError = new TypeError("Some type error");
const theSyntaxError = new SyntaxError("Some syntax error");
const theRangeError = new RangeError("Some type error");
const theReferenceError = new ReferenceError("Some reference error");
const theURIError = new URIError("Some URI error");
const theEvalError = new EvalError("Some eval error");
function assertThrowsWithCode(fn, value) {
let thrown = false;
try {
fn();
} catch (e) {
thrown = true;
assertEquals(e.message, value.message);
assertEquals(e.code, value.code);
} finally {
assert(thrown);
}
}
Deno.test("napi error", function () {
class MyError extends Error {}
const myError = new MyError("Some MyError");
// Test that native error object is correctly classed
assertEquals(testError.checkError(theError), true);
// Test that native type error object is correctly classed
assertEquals(testError.checkError(theTypeError), true);
// Test that native syntax error object is correctly classed
assertEquals(testError.checkError(theSyntaxError), true);
// Test that native range error object is correctly classed
assertEquals(testError.checkError(theRangeError), true);
// Test that native reference error object is correctly classed
assertEquals(testError.checkError(theReferenceError), true);
// Test that native URI error object is correctly classed
assertEquals(testError.checkError(theURIError), true);
// Test that native eval error object is correctly classed
assertEquals(testError.checkError(theEvalError), true);
// Test that class derived from native error is correctly classed
assertEquals(testError.checkError(myError), true);
// Test that non-error object is correctly classed
assertEquals(testError.checkError({}), false);
// Test that non-error primitive is correctly classed
assertEquals(testError.checkError("non-object"), false);
assertThrows(
() => {
testError.throwExistingError();
},
Error,
"existing error",
);
assertThrows(
() => {
testError.throwError();
},
Error,
"error",
);
assertThrows(
() => {
testError.throwRangeError();
},
RangeError,
"range error",
);
assertThrows(
() => {
testError.throwTypeError();
},
TypeError,
"type error",
);
// assertThrows(() => {
// testError.throwSyntaxError();
// }, "SyntaxError: syntax error");
[42, {}, [], Symbol("xyzzy"), true, "ball", undefined, null, NaN]
.forEach((value) => {
let thrown = false;
try {
testError.throwArbitrary(value);
} catch (e) {
thrown = true;
assertEquals(e, value);
} finally {
assert(thrown);
}
});
assertThrowsWithCode(
() => testError.throwErrorCode(),
{
code: "ERR_TEST_CODE",
message: "Error [error]",
},
);
assertThrowsWithCode(
() => testError.throwRangeErrorCode(),
{
code: "ERR_TEST_CODE",
message: "RangeError [range error]",
},
);
assertThrowsWithCode(
() => testError.throwTypeErrorCode(),
{
code: "ERR_TEST_CODE",
message: "TypeError [type error]",
},
);
// assertThrowsWithCode(
// () => testError.throwSyntaxErrorCode(),
// {
// code: "ERR_TEST_CODE",
// message: "SyntaxError [syntax error]",
// },
// );
let error = testError.createError();
assert(
error instanceof Error,
"expected error to be an instance of Error",
);
assertEquals(error.message, "error");
error = testError.createRangeError();
assert(
error instanceof RangeError,
"expected error to be an instance of RangeError",
);
assertEquals(error.message, "range error");
error = testError.createTypeError();
assert(
error instanceof TypeError,
"expected error to be an instance of TypeError",
);
assertEquals(error.message, "type error");
// TODO(bartlomieju): this is experimental API
// error = testError.createSyntaxError();
// assert(
// error instanceof SyntaxError,
// "expected error to be an instance of SyntaxError",
// );
// assertEquals(error.message, "syntax error");
error = testError.createErrorCode();
assert(
error instanceof Error,
"expected error to be an instance of Error",
);
assertEquals(error.code, "ERR_TEST_CODE");
assertEquals(error.message, "Error [error]");
assertEquals(error.name, "Error");
error = testError.createRangeErrorCode();
assert(
error instanceof RangeError,
"expected error to be an instance of RangeError",
);
assertEquals(error.message, "RangeError [range error]");
assertEquals(error.code, "ERR_TEST_CODE");
assertEquals(error.name, "RangeError");
error = testError.createTypeErrorCode();
assert(
error instanceof TypeError,
"expected error to be an instance of TypeError",
);
assertEquals(error.message, "TypeError [type error]");
assertEquals(error.code, "ERR_TEST_CODE");
assertEquals(error.name, "TypeError");
// TODO(bartlomieju): this is experimental API
// error = testError.createSyntaxErrorCode();
// assert(
// error instanceof SyntaxError,
// "expected error to be an instance of SyntaxError",
// );
// assertEquals(error.message, "SyntaxError [syntax error]");
// assertEquals(error.code, "ERR_TEST_CODE");
// assertEquals(error.name, "SyntaxError");
});

288
test_napi/src/error.rs Normal file
View file

@ -0,0 +1,288 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use crate::assert_napi_ok;
use crate::cstr;
use crate::napi_get_callback_info;
use crate::napi_new_property;
use napi_sys::*;
use std::ptr;
extern "C" fn check_error(
env: napi_env,
info: napi_callback_info,
) -> napi_value {
let (args, argc, _) = napi_get_callback_info!(env, info, 1);
assert_eq!(argc, 1);
let mut r = false;
assert_napi_ok!(napi_is_error(env, args[0], &mut r));
let mut result: napi_value = ptr::null_mut();
assert_napi_ok!(napi_get_boolean(env, r, &mut result));
result
}
extern "C" fn create_error(
env: napi_env,
_info: napi_callback_info,
) -> napi_value {
let mut result: napi_value = ptr::null_mut();
let mut message: napi_value = ptr::null_mut();
assert_napi_ok!(napi_create_string_utf8(
env,
cstr!("error"),
usize::MAX,
&mut message
));
assert_napi_ok!(napi_create_error(
env,
ptr::null_mut(),
message,
&mut result
));
result
}
extern "C" fn create_range_error(
env: napi_env,
_info: napi_callback_info,
) -> napi_value {
let mut result: napi_value = ptr::null_mut();
let mut message: napi_value = ptr::null_mut();
assert_napi_ok!(napi_create_string_utf8(
env,
cstr!("range error"),
usize::MAX,
&mut message
));
assert_napi_ok!(napi_create_range_error(
env,
ptr::null_mut(),
message,
&mut result
));
result
}
extern "C" fn create_type_error(
env: napi_env,
_info: napi_callback_info,
) -> napi_value {
let mut result: napi_value = ptr::null_mut();
let mut message: napi_value = ptr::null_mut();
assert_napi_ok!(napi_create_string_utf8(
env,
cstr!("type error"),
usize::MAX,
&mut message
));
assert_napi_ok!(napi_create_type_error(
env,
ptr::null_mut(),
message,
&mut result
));
result
}
extern "C" fn create_error_code(
env: napi_env,
_info: napi_callback_info,
) -> napi_value {
let mut result: napi_value = ptr::null_mut();
let mut message: napi_value = ptr::null_mut();
let mut code: napi_value = ptr::null_mut();
assert_napi_ok!(napi_create_string_utf8(
env,
cstr!("Error [error]"),
usize::MAX,
&mut message
));
assert_napi_ok!(napi_create_string_utf8(
env,
cstr!("ERR_TEST_CODE"),
usize::MAX,
&mut code
));
assert_napi_ok!(napi_create_error(env, code, message, &mut result));
result
}
extern "C" fn create_range_error_code(
env: napi_env,
_info: napi_callback_info,
) -> napi_value {
let mut result: napi_value = ptr::null_mut();
let mut message: napi_value = ptr::null_mut();
let mut code: napi_value = ptr::null_mut();
assert_napi_ok!(napi_create_string_utf8(
env,
cstr!("RangeError [range error]"),
usize::MAX,
&mut message
));
assert_napi_ok!(napi_create_string_utf8(
env,
cstr!("ERR_TEST_CODE"),
usize::MAX,
&mut code
));
assert_napi_ok!(napi_create_range_error(env, code, message, &mut result));
result
}
extern "C" fn create_type_error_code(
env: napi_env,
_info: napi_callback_info,
) -> napi_value {
let mut result: napi_value = ptr::null_mut();
let mut message: napi_value = ptr::null_mut();
let mut code: napi_value = ptr::null_mut();
assert_napi_ok!(napi_create_string_utf8(
env,
cstr!("TypeError [type error]"),
usize::MAX,
&mut message
));
assert_napi_ok!(napi_create_string_utf8(
env,
cstr!("ERR_TEST_CODE"),
usize::MAX,
&mut code
));
assert_napi_ok!(napi_create_type_error(env, code, message, &mut result));
result
}
extern "C" fn throw_existing_error(
env: napi_env,
_info: napi_callback_info,
) -> napi_value {
let mut message: napi_value = ptr::null_mut();
let mut error: napi_value = ptr::null_mut();
assert_napi_ok!(napi_create_string_utf8(
env,
cstr!("existing error"),
usize::MAX,
&mut message
));
assert_napi_ok!(napi_create_error(
env,
std::ptr::null_mut(),
message,
&mut error
));
assert_napi_ok!(napi_throw(env, error));
std::ptr::null_mut()
}
extern "C" fn throw_error(
env: napi_env,
_info: napi_callback_info,
) -> napi_value {
assert_napi_ok!(napi_throw_error(env, std::ptr::null_mut(), cstr!("error"),));
std::ptr::null_mut()
}
extern "C" fn throw_range_error(
env: napi_env,
_info: napi_callback_info,
) -> napi_value {
assert_napi_ok!(napi_throw_range_error(
env,
std::ptr::null_mut(),
cstr!("range error"),
));
std::ptr::null_mut()
}
extern "C" fn throw_type_error(
env: napi_env,
_info: napi_callback_info,
) -> napi_value {
assert_napi_ok!(napi_throw_type_error(
env,
std::ptr::null_mut(),
cstr!("type error"),
));
std::ptr::null_mut()
}
extern "C" fn throw_arbitrary(
env: napi_env,
info: napi_callback_info,
) -> napi_value {
let (args, argc, _) = napi_get_callback_info!(env, info, 1);
assert_eq!(argc, 1);
assert_napi_ok!(napi_throw(env, args[0]));
std::ptr::null_mut()
}
extern "C" fn throw_error_code(
env: napi_env,
_info: napi_callback_info,
) -> napi_value {
assert_napi_ok!(napi_throw_error(
env,
cstr!("ERR_TEST_CODE"),
cstr!("Error [error]"),
));
std::ptr::null_mut()
}
extern "C" fn throw_range_error_code(
env: napi_env,
_info: napi_callback_info,
) -> napi_value {
assert_napi_ok!(napi_throw_range_error(
env,
cstr!("ERR_TEST_CODE"),
cstr!("RangeError [range error]"),
));
std::ptr::null_mut()
}
extern "C" fn throw_type_error_code(
env: napi_env,
_info: napi_callback_info,
) -> napi_value {
assert_napi_ok!(napi_throw_type_error(
env,
cstr!("ERR_TEST_CODE"),
cstr!("TypeError [type error]"),
));
std::ptr::null_mut()
}
pub fn init(env: napi_env, exports: napi_value) {
let properties = &[
napi_new_property!(env, "checkError", check_error),
napi_new_property!(env, "throwExistingError", throw_existing_error),
napi_new_property!(env, "throwError", throw_error),
napi_new_property!(env, "throwRangeError", throw_range_error),
napi_new_property!(env, "throwTypeError", throw_type_error),
// NOTE(bartlomieju): currently experimental api
// napi_new_property!(env, "throwSyntaxError", throw_syntax_error),
napi_new_property!(env, "throwErrorCode", throw_error_code),
napi_new_property!(env, "throwRangeErrorCode", throw_range_error_code),
napi_new_property!(env, "throwTypeErrorCode", throw_type_error_code),
// NOTE(bartlomieju): currently experimental api
// napi_new_property!(env, "throwSyntaxErrorCode", throw_syntax_error_code),
napi_new_property!(env, "throwArbitrary", throw_arbitrary),
napi_new_property!(env, "createError", create_error),
napi_new_property!(env, "createRangeError", create_range_error),
napi_new_property!(env, "createTypeError", create_type_error),
// NOTE(bartlomieju): currently experimental api
// napi_new_property!(env, "createSyntaxError", create_syntax_error),
napi_new_property!(env, "createErrorCode", create_error_code),
napi_new_property!(env, "createRangeErrorCode", create_range_error_code),
napi_new_property!(env, "createTypeErrorCode", create_type_error_code),
// NOTE(bartlomieju): currently experimental api
// napi_new_property!(env, "createSyntaxErrorCode", create_syntax_error_code),
];
assert_napi_ok!(napi_define_properties(
env,
exports,
properties.len(),
properties.as_ptr()
));
}

View file

@ -12,6 +12,7 @@ pub mod r#async;
pub mod callback;
pub mod coerce;
pub mod date;
pub mod error;
pub mod numbers;
pub mod object_wrap;
pub mod primitives;
@ -21,6 +22,13 @@ pub mod strings;
pub mod tsfn;
pub mod typedarray;
#[macro_export]
macro_rules! cstr {
($s: literal) => {{
std::ffi::CString::new($s).unwrap().as_ptr()
}};
}
#[macro_export]
macro_rules! assert_napi_ok {
($call: expr) => {{
@ -127,6 +135,7 @@ unsafe extern "C" fn napi_register_module_v1(
typedarray::init(env, exports);
arraybuffer::init(env, exports);
array::init(env, exports);
error::init(env, exports);
primitives::init(env, exports);
properties::init(env, exports);
promise::init(env, exports);