Skip to content

Commit

Permalink
Remove TryFromJs on Error
Browse files Browse the repository at this point in the history
  • Loading branch information
kjvalencik committed May 10, 2024
1 parent 9dce892 commit 6c655cf
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 70 deletions.
2 changes: 1 addition & 1 deletion crates/neon/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
74 changes: 5 additions & 69 deletions crates/neon/src/types_impl/extract/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn error::Error + Send + Sync + 'static>;

#[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.
Expand All @@ -30,10 +25,6 @@ type BoxError = Box<dyn error::Error + Send + Sync + 'static>;
/// 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<ErrorKind>,
Expand Down Expand Up @@ -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;

Expand All @@ -136,58 +127,3 @@ impl<'cx> TryIntoJs<'cx> for Error {
}
}
}

impl<'cx> TryFromJs<'cx> for Error {
type Error = TypeExpected<JsError>;

fn try_from_js<C>(cx: &mut C, v: Handle<'cx, JsValue>) -> NeonResult<Result<Self, Self::Error>>
where
C: Context<'cx>,
{
let err = match v.downcast::<JsError, _>(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<C>(cx: &mut C, v: Handle<'cx, JsValue>) -> NeonResult<Self>
where
C: Context<'cx>,
{
Self::try_from_js(cx, v)?.or_throw(cx)
}
}

fn get_message<'cx, C>(cx: &mut C, err: Handle<JsError>) -> NeonResult<String>
where
C: Context<'cx>,
{
let message = err
.get_value(cx, "message")?
.downcast::<JsString, _>(cx)
.map(|v| v.value(cx))
.unwrap_or_default();

Ok(message)
}

fn get_kind<'cx, C>(cx: &mut C, err: Handle<JsError>) -> NeonResult<Option<ErrorKind>>
where
C: Context<'cx>,
{
let name = match err.get_value(cx, "name")?.downcast::<JsString, _>(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),
}))
}

0 comments on commit 6c655cf

Please sign in to comment.