pub struct JsError { /* private fields */ }Expand description
The error type returned by all operations related to the execution of Javascript code.
This is essentially an enum that can store either JsNativeErrors (for ideal
native errors) or opaque JsValues, since Javascript allows throwing any valid
JsValue.
The implementation doesn’t provide a From conversion
for JsValue. This is with the intent of encouraging the usage of proper
JsNativeErrors instead of plain JsValues. However, if you
do need a proper opaque error, you can construct one using the
JsError::from_opaque method.
§Examples
let cause = JsError::from_opaque(js_string!("error!").into());
assert!(cause.as_opaque().is_some());
assert_eq!(
cause.as_opaque().unwrap(),
&JsValue::from(js_string!("error!"))
);
let native_error: JsError = JsNativeError::typ()
.with_message("invalid type!")
.with_cause(cause)
.into();
assert!(native_error.as_native().is_some());
let kind = &native_error.as_native().unwrap().kind;
assert!(matches!(kind, JsNativeErrorKind::Type));Implementations§
Source§impl JsError
impl JsError
Sourcepub fn from_native(err: JsNativeError) -> Self
pub fn from_native(err: JsNativeError) -> Self
Creates a new JsError from a native error err.
§Examples
let error = JsError::from_native(JsNativeError::syntax());
assert!(error.as_native().is_some());Sourcepub fn from_rust(err: impl Error) -> Self
pub fn from_rust(err: impl Error) -> Self
Creates a new JsError from a Rust standard error err.
This will create a new JsNativeError with the message of the standard error.
§Examples
let error = std::io::Error::new(std::io::ErrorKind::Other, "oh no!");
let js_error: JsError = JsError::from_rust(error);
assert_eq!(js_error.as_native().unwrap().message(), "oh no!");
assert!(js_error.as_native().unwrap().cause().is_none());Sourcepub const fn from_opaque(value: JsValue) -> Self
pub const fn from_opaque(value: JsValue) -> Self
Creates a new JsError from an opaque error value.
§Examples
let error = JsError::from_opaque(5.0f64.into());
assert!(error.as_opaque().is_some());Sourcepub fn to_opaque(&self, context: &mut Context) -> JsValue
pub fn to_opaque(&self, context: &mut Context) -> JsValue
Converts the error to an opaque JsValue error
Unwraps the inner JsValue if the error is already an opaque error.
§Examples
let context = &mut Context::default();
let error: JsError =
JsNativeError::eval().with_message("invalid script").into();
let error_val = error.to_opaque(context);
assert!(error_val.as_object().unwrap().is::<Error>());Sourcepub fn try_native(
&self,
context: &mut Context,
) -> Result<JsNativeError, TryNativeError>
pub fn try_native( &self, context: &mut Context, ) -> Result<JsNativeError, TryNativeError>
Unwraps the inner error if this contains a native error.
Otherwise, inspects the opaque error and tries to extract the
necessary information to construct a native error similar to the provided
opaque error. If the conversion fails, returns a TryNativeError
with the cause of the failure.
§Note 1
This method won’t try to make any conversions between JS types. In other words, for this conversion to succeed:
messageMUST be aJsStringvalue.errors(in the case ofAggregateErrors) MUST be anArrayobject.
§Note 2
This operation should be considered a lossy conversion, since it
won’t store any additional properties of the opaque
error, other than message, cause and errors (in the case of
AggregateErrors). If you cannot affort a lossy conversion, clone
the object before calling from_opaque
to preserve its original properties.
§Examples
let context = &mut Context::default();
// create a new, opaque Error object
let error: JsError = JsNativeError::typ().with_message("type error!").into();
let error_val = error.to_opaque(context);
// then, try to recover the original
let error = JsError::from_opaque(error_val).try_native(context).unwrap();
assert!(matches!(error.kind, JsNativeErrorKind::Type));
assert_eq!(error.message(), "type error!");Sourcepub const fn as_opaque(&self) -> Option<&JsValue>
pub const fn as_opaque(&self) -> Option<&JsValue>
Gets the inner JsValue if the error is an opaque error,
or None otherwise.
§Examples
let error: JsError = JsNativeError::reference()
.with_message("variable not found!")
.into();
assert!(error.as_opaque().is_none());
let error = JsError::from_opaque(256u32.into());
assert!(error.as_opaque().is_some());Sourcepub const fn as_native(&self) -> Option<&JsNativeError>
pub const fn as_native(&self) -> Option<&JsNativeError>
Gets the inner JsNativeError if the error is a native
error, or None otherwise.
§Examples
let error: JsError =
JsNativeError::error().with_message("Unknown error").into();
assert!(error.as_native().is_some());
let error = JsError::from_opaque(JsValue::undefined());
assert!(error.as_native().is_none());Sourcepub fn into_erased(self, context: &mut Context) -> JsErasedError
pub fn into_erased(self, context: &mut Context) -> JsErasedError
Converts this error into its thread-safe, erased version.
Even though this operation is lossy, converting into a JsErasedError
is useful since it implements Send and Sync, making it compatible with
error reporting frameworks such as anyhow, eyre or miette.
§Examples
let context = &mut Context::default();
let cause = JsError::from_opaque(JsSymbol::new(Some(js_string!("error!"))).unwrap().into());
let native_error: JsError = JsNativeError::typ()
.with_message("invalid type!")
.with_cause(cause)
.into();
let erased_error = native_error.into_erased(context);
assert_eq!(erased_error.to_string(), "TypeError: invalid type!");
let send_sync_error: Box<dyn Error + Send + Sync> = Box::new(erased_error);
assert_eq!(
send_sync_error.source().unwrap().to_string(),
"Symbol(error!)"
);Trait Implementations§
Source§impl Error for JsError
impl Error for JsError
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
Source§impl From<JsNativeError> for JsError
impl From<JsNativeError> for JsError
Source§fn from(error: JsNativeError) -> Self
fn from(error: JsNativeError) -> Self
Source§impl From<TemporalError> for JsError
impl From<TemporalError> for JsError
Source§fn from(value: TemporalError) -> Self
fn from(value: TemporalError) -> Self
Source§impl Trace for JsError
impl Trace for JsError
Source§unsafe fn trace_non_roots(&self)
unsafe fn trace_non_roots(&self)
Source§fn run_finalizer(&self)
fn run_finalizer(&self)
Finalize::finalize on this object and all
contained subobjects.impl Eq for JsError
Auto Trait Implementations§
impl Freeze for JsError
impl !RefUnwindSafe for JsError
impl !Send for JsError
impl !Sync for JsError
impl Unpin for JsError
impl !UnwindSafe for JsError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.