chore(napi): including type message in error message (#1350)
This commit is contained in:
parent
1037e6f14d
commit
3dde26bcef
3 changed files with 44 additions and 10 deletions
|
@ -1,4 +1,4 @@
|
|||
use crate::{bindgen_prelude::*, check_status, sys, Error, Result, Status};
|
||||
use crate::{bindgen_prelude::*, check_status, check_status_and_type, sys, Error, Result, Status};
|
||||
|
||||
use std::ffi::{c_void, CStr};
|
||||
use std::fmt::Display;
|
||||
|
@ -35,9 +35,11 @@ impl FromNapiValue for String {
|
|||
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
|
||||
let mut len = 0;
|
||||
|
||||
check_status!(
|
||||
check_status_and_type!(
|
||||
unsafe { sys::napi_get_value_string_utf8(env, napi_val, ptr::null_mut(), 0, &mut len) },
|
||||
"Failed to convert napi `string` into rust type `String`",
|
||||
env,
|
||||
napi_val,
|
||||
"Failed to convert napi `{}` into rust type `String`"
|
||||
)?;
|
||||
|
||||
// end char len in C
|
||||
|
@ -47,11 +49,13 @@ impl FromNapiValue for String {
|
|||
|
||||
let mut written_char_count = 0;
|
||||
|
||||
check_status!(
|
||||
check_status_and_type!(
|
||||
unsafe {
|
||||
sys::napi_get_value_string_utf8(env, napi_val, buf_ptr, len, &mut written_char_count)
|
||||
},
|
||||
"Failed to convert napi `string` into rust type `String`"
|
||||
env,
|
||||
napi_val,
|
||||
"Failed to convert napi `{}` into rust type `String`"
|
||||
)?;
|
||||
|
||||
let mut ret = mem::ManuallyDrop::new(ret);
|
||||
|
@ -83,9 +87,11 @@ impl FromNapiValue for &str {
|
|||
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> Result<Self> {
|
||||
let mut len = 0;
|
||||
|
||||
check_status!(
|
||||
check_status_and_type!(
|
||||
unsafe { sys::napi_get_value_string_utf8(env, napi_val, ptr::null_mut(), 0, &mut len) },
|
||||
"Failed to convert napi `string` into rust type `String`",
|
||||
env,
|
||||
napi_val,
|
||||
"Failed to convert napi `{}` into rust type `String`"
|
||||
)?;
|
||||
|
||||
// end char len in C
|
||||
|
@ -94,11 +100,13 @@ impl FromNapiValue for &str {
|
|||
let buf_ptr = ret.as_mut_ptr();
|
||||
let mut written_char_count = 0;
|
||||
|
||||
check_status!(
|
||||
check_status_and_type!(
|
||||
unsafe {
|
||||
sys::napi_get_value_string_utf8(env, napi_val, buf_ptr, len, &mut written_char_count)
|
||||
},
|
||||
"Failed to convert napi `string` into rust type `String`"
|
||||
env,
|
||||
napi_val,
|
||||
"Failed to convert napi `{}` into rust type `String`"
|
||||
)?;
|
||||
|
||||
// The `&str` should only be accepted from function arguments.
|
||||
|
|
|
@ -351,6 +351,32 @@ macro_rules! check_status {
|
|||
_ => Err($crate::Error::new($crate::Status::from(c), format!($($msg)*))),
|
||||
}
|
||||
}};
|
||||
|
||||
($code:expr, $msg:expr, $env:expr, $val:expr) => {{
|
||||
let c = $code;
|
||||
match c {
|
||||
$crate::sys::Status::napi_ok => Ok(()),
|
||||
_ => Err($crate::Error::new($crate::Status::from(c), format!($msg, $crate::type_of!($env, $val)?))),
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
#[macro_export]
|
||||
macro_rules! check_status_and_type {
|
||||
($code:expr, $env:ident, $val:ident, $msg:expr) => {{
|
||||
let c = $code;
|
||||
match c {
|
||||
$crate::sys::Status::napi_ok => Ok(()),
|
||||
_ => {
|
||||
let value_type = $crate::type_of!($env, $val)?;
|
||||
Err($crate::Error::new(
|
||||
$crate::Status::from(c),
|
||||
format!($msg, value_type),
|
||||
))
|
||||
}
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
|
|
|
@ -414,7 +414,7 @@ test('option object', (t) => {
|
|||
test('should throw if object type is not matched', (t) => {
|
||||
// @ts-expect-error
|
||||
const err1 = t.throws(() => receiveStrictObject({ name: 1 }))
|
||||
t.is(err1!.message, 'Failed to convert napi `string` into rust type `String`')
|
||||
t.is(err1!.message, 'Failed to convert napi `Number` into rust type `String`')
|
||||
// @ts-expect-error
|
||||
const err2 = t.throws(() => receiveStrictObject({ bar: 1 }))
|
||||
t.is(err2!.message, 'Missing field `name`')
|
||||
|
|
Loading…
Reference in a new issue