diff --git a/crates/neon/src/macros.rs b/crates/neon/src/macros.rs index 68cc2a131..bab7f086a 100644 --- a/crates/neon/src/macros.rs +++ b/crates/neon/src/macros.rs @@ -180,7 +180,7 @@ pub use neon_macros::main; /// #### `context` /// /// The `#[neon::export]` macro looks checks if the first argument has a type of -/// &mut FunctionContext` to determine if the [`Context`](crate::context::Context) +/// `&mut FunctionContext` to determine if the [`Context`](crate::context::Context) /// should be passed to the function. /// /// If the type has been renamed when importing, the `context` attribute can be diff --git a/crates/neon/src/types_impl/extract/error.rs b/crates/neon/src/types_impl/extract/error.rs index b39869b82..51b1c2105 100644 --- a/crates/neon/src/types_impl/extract/error.rs +++ b/crates/neon/src/types_impl/extract/error.rs @@ -2,19 +2,14 @@ use std::{error, fmt}; use crate::{ context::Context, - handle::Handle, - object::Object, - result::{JsResult, NeonResult, ResultExt}, - types::{ - extract::{TryFromJs, TryIntoJs, TypeExpected}, - JsError, JsString, JsValue, - }, + result::JsResult, + types::{extract::TryIntoJs, JsError}, }; type BoxError = Box; #[derive(Debug)] -/// Error that implements [`TryFromJs`] and [`TryIntoJs`] and can produce specific error types +/// Error that implements [`TryIntoJs`] and can produce specific error types. /// /// [`Error`] implements [`From`] for most error types, allowing ergonomic error handling in /// exported functions with the `?` operator. @@ -30,10 +25,6 @@ type BoxError = Box; /// Ok(contents) /// } /// ``` -/// -/// **Note**: Extracting an [`Error`] from a [`JsValue`] with [`TryFromJs`] and converting -/// back to a [`JsError`] with [`TryIntoJs`] is _lossy_. It is not guaranteed that the same -/// type will be returned. pub struct Error { cause: BoxError, kind: Option, @@ -118,8 +109,8 @@ impl fmt::Display for Error { } } -// `TryFromJs` followed by `TryIntoJs` is *lossy*. We cannot guarantee the same type -// will be created. +// N.B.: `TryFromJs` is not included. If Neon were to add support for additional error types, +// this would be a *breaking* change. We will wait for user demand before providing this feature. impl<'cx> TryIntoJs<'cx> for Error { type Value = JsError; @@ -136,58 +127,3 @@ impl<'cx> TryIntoJs<'cx> for Error { } } } - -impl<'cx> TryFromJs<'cx> for Error { - type Error = TypeExpected; - - fn try_from_js(cx: &mut C, v: Handle<'cx, JsValue>) -> NeonResult> - where - C: Context<'cx>, - { - let err = match v.downcast::(cx) { - Ok(err) => err, - Err(_) => return Ok(Err(Self::Error::new())), - }; - - Ok(Ok(Self { - cause: get_message(cx, err)?.into(), - kind: get_kind(cx, err)?, - })) - } - - fn from_js(cx: &mut C, v: Handle<'cx, JsValue>) -> NeonResult - where - C: Context<'cx>, - { - Self::try_from_js(cx, v)?.or_throw(cx) - } -} - -fn get_message<'cx, C>(cx: &mut C, err: Handle) -> NeonResult -where - C: Context<'cx>, -{ - let message = err - .get_value(cx, "message")? - .downcast::(cx) - .map(|v| v.value(cx)) - .unwrap_or_default(); - - Ok(message) -} - -fn get_kind<'cx, C>(cx: &mut C, err: Handle) -> NeonResult> -where - C: Context<'cx>, -{ - let name = match err.get_value(cx, "name")?.downcast::(cx) { - Ok(v) => v.value(cx), - Err(_) => return Ok(None), - }; - - Ok(Some(match name.as_str() { - "TypeError" => ErrorKind::TypeError, - "RangeError" => ErrorKind::RangeError, - _ => return Ok(None), - })) -}