pub struct JsError {/* private fields */ }Expand description
The error type returned by all operations relatedto the execution of Javascript code.
This is essentially an enum that can store eitherJsNativeErrors (for idealnative errors) or opaqueJsValues, since Javascript allows throwing any validJsValue.
The implementation doesn’t provide aFrom conversionforJsValue. This is with the intent of encouraging the usage of properJsNativeErrors instead of plainJsValues. However, if youdo need a proper opaque error, you can construct one using theJsError::from_opaque method.
§Examples
letcause = JsError::from_opaque(js_string!("error!").into());assert!(cause.as_opaque().is_some());assert_eq!( cause.as_opaque().unwrap(),&JsValue::from(js_string!("error!")));letnative_error: JsError = JsNativeError::typ() .with_message("invalid type!") .with_cause(cause) .into();assert!(native_error.as_native().is_some());letkind =&native_error.as_native().unwrap().kind;assert!(matches!(kind, JsNativeErrorKind::Type));Implementations§
Source§implJsError
implJsError
Sourcepub fnfrom_native(err:JsNativeError) -> Self
pub fnfrom_native(err:JsNativeError) -> Self
Creates a newJsError from a native errorerr.
§Examples
leterror = JsError::from_native(JsNativeError::syntax());assert!(error.as_native().is_some());Sourcepub fnfrom_rust(err: implError) -> Self
pub fnfrom_rust(err: implError) -> Self
Creates a newJsError from a Rust standard errorerr.This will create a newJsNativeError with the message of the standard error.
§Examples
leterror = std::io::Error::new(std::io::ErrorKind::Other,"oh no!");letjs_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 fnfrom_opaque(value:JsValue) -> Self
pub const fnfrom_opaque(value:JsValue) -> Self
Creates a newJsError from an opaque errorvalue.
§Examples
leterror = JsError::from_opaque(5.0f64.into());assert!(error.as_opaque().is_some());Sourcepub fnto_opaque(&self, context: &mutContext) ->JsValue
pub fnto_opaque(&self, context: &mutContext) ->JsValue
Converts the error to an opaqueJsValue error
Unwraps the innerJsValue if the error is already an opaque error.
§Examples
letcontext =&mutContext::default();leterror: JsError = JsNativeError::eval().with_message("invalid script").into();leterror_val = error.to_opaque(context);assert!(error_val.as_object().unwrap().is::<Error>());Sourcepub fntry_native( &self, context: &mutContext,) ->Result<JsNativeError,TryNativeError>
pub fntry_native( &self, context: &mutContext,) ->Result<JsNativeError,TryNativeError>
Unwraps the inner error if this contains a native error.Otherwise, inspects the opaque error and tries to extract thenecessary information to construct a native error similar to the providedopaque error. If the conversion fails, returns aTryNativeErrorwith 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 itwon’t store any additional properties of the opaqueerror, other thanmessage,cause anderrors (in the case ofAggregateErrors). If you cannot affort a lossy conversion, clonethe object before callingfrom_opaqueto preserve its original properties.
§Examples
letcontext =&mutContext::default();// create a new, opaque Error objectleterror: JsError = JsNativeError::typ().with_message("type error!").into();leterror_val = error.to_opaque(context);// then, try to recover the originalleterror = JsError::from_opaque(error_val).try_native(context).unwrap();assert!(matches!(error.kind, JsNativeErrorKind::Type));assert_eq!(error.message(),"type error!");Sourcepub const fnas_native(&self) ->Option<&JsNativeError>
pub const fnas_native(&self) ->Option<&JsNativeError>
Gets the innerJsNativeError if the error is a nativeerror, orNone otherwise.
§Examples
leterror: JsError = JsNativeError::error().with_message("Unknown error").into();assert!(error.as_native().is_some());leterror = JsError::from_opaque(JsValue::undefined());assert!(error.as_native().is_none());Sourcepub fninto_erased(self, context: &mutContext) ->JsErasedError
pub fninto_erased(self, context: &mutContext) ->JsErasedError
Converts this error into its thread-safe, erased version.
Even though this operation is lossy, converting into aJsErasedErroris useful since it implementsSend andSync, making it compatible witherror reporting frameworks such asanyhow,eyre ormiette.
§Examples
letcontext =&mutContext::default();letcause = JsError::from_opaque(JsSymbol::new(Some(js_string!("error!"))).unwrap().into());letnative_error: JsError = JsNativeError::typ() .with_message("invalid type!") .with_cause(cause) .into();leterased_error = native_error.into_erased(context);assert_eq!(erased_error.to_string(),"TypeError: invalid type!");letsend_sync_error: Box<dynError + Send + Sync> = Box::new(erased_error);assert_eq!( send_sync_error.source().unwrap().to_string(),"Symbol(error!)");Trait Implementations§
Source§implError forJsError
implError forJsError
Source§fnsource(&self) ->Option<&(dynError + 'static)>
fnsource(&self) ->Option<&(dynError + 'static)>
1.0.0 ·Source§fndescription(&self) -> &str
fndescription(&self) -> &str
Source§implFrom<JsNativeError> forJsError
implFrom<JsNativeError> forJsError
Source§fnfrom(error:JsNativeError) -> Self
fnfrom(error:JsNativeError) -> Self
Source§implFrom<TemporalError> forJsError
implFrom<TemporalError> forJsError
Source§fnfrom(value:TemporalError) -> Self
fnfrom(value:TemporalError) -> Self
Source§implTrace forJsError
implTrace forJsError
Source§unsafe fntrace_non_roots(&self)
unsafe fntrace_non_roots(&self)
Source§fnrun_finalizer(&self)
fnrun_finalizer(&self)
Finalize::finalize on this object and allcontained subobjects.implEq forJsError
Auto Trait Implementations§
implFreeze forJsError
impl !RefUnwindSafe forJsError
impl !Send forJsError
impl !Sync forJsError
implUnpin forJsError
impl !UnwindSafe forJsError
Blanket Implementations§
Source§impl<T>BorrowMut<T> for Twhere T: ?Sized,
impl<T>BorrowMut<T> for Twhere T: ?Sized,
Source§fnborrow_mut(&mut self) ->&mut T
fnborrow_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§fnequivalent(&self, key:&K) ->bool
fnequivalent(&self, key:&K) ->bool
key and returntrue 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§fninto_either(self, into_left:bool) ->Either<Self, Self>
fninto_either(self, into_left:bool) ->Either<Self, Self>
self into aLeft variant ofEither<Self, Self>ifinto_left istrue.Convertsself into aRight variant ofEither<Self, Self>otherwise.Read moreSource§fninto_either_with<F>(self, into_left: F) ->Either<Self, Self>
fninto_either_with<F>(self, into_left: F) ->Either<Self, Self>
self into aLeft variant ofEither<Self, Self>ifinto_left(&self) returnstrue.Convertsself into aRight variant ofEither<Self, Self>otherwise.Read moreSource§impl<T>Pipe for Twhere T: ?Sized,
impl<T>Pipe for Twhere T: ?Sized,
Source§fnpipe<R>(self, func: implFnOnce(Self) -> R) -> Rwhere Self:Sized,
fnpipe<R>(self, func: implFnOnce(Self) -> R) -> Rwhere Self:Sized,
Source§fnpipe_ref<'a, R>(&'a self, func: implFnOnce(&'a Self) -> R) -> Rwhere R: 'a,
fnpipe_ref<'a, R>(&'a self, func: implFnOnce(&'a Self) -> R) -> Rwhere R: 'a,
self and passes that borrow into the pipe function.Read moreSource§fnpipe_ref_mut<'a, R>(&'a mut self, func: implFnOnce(&'a mut Self) -> R) -> Rwhere R: 'a,
fnpipe_ref_mut<'a, R>(&'a mut self, func: implFnOnce(&'a mut Self) -> R) -> Rwhere R: 'a,
self and passes that borrow into the pipe function.Read moreSource§fnpipe_borrow<'a, B, R>(&'a self, func: implFnOnce(&'a B) -> R) -> R
fnpipe_borrow<'a, B, R>(&'a self, func: implFnOnce(&'a B) -> R) -> R
Source§fnpipe_borrow_mut<'a, B, R>( &'a mut self, func: implFnOnce(&'a mut B) -> R,) -> R
fnpipe_borrow_mut<'a, B, R>( &'a mut self, func: implFnOnce(&'a mut B) -> R,) -> R
Source§fnpipe_as_ref<'a, U, R>(&'a self, func: implFnOnce(&'a U) -> R) -> R
fnpipe_as_ref<'a, U, R>(&'a self, func: implFnOnce(&'a U) -> R) -> R
self, then passesself.as_ref() into the pipe function.Source§fnpipe_as_mut<'a, U, R>(&'a mut self, func: implFnOnce(&'a mut U) -> R) -> R
fnpipe_as_mut<'a, U, R>(&'a mut self, func: implFnOnce(&'a mut U) -> R) -> R
self, then passesself.as_mut() into the pipefunction.Source§fnpipe_deref<'a, T, R>(&'a self, func: implFnOnce(&'a T) -> R) -> R
fnpipe_deref<'a, T, R>(&'a self, func: implFnOnce(&'a T) -> R) -> R
self, then passesself.deref() into the pipe function.Source§impl<T>Tap for T
impl<T>Tap for T
Source§fntap_borrow<B>(self, func: implFnOnce(&B)) -> Self
fntap_borrow<B>(self, func: implFnOnce(&B)) -> Self
Borrow<B> of a value.Read moreSource§fntap_borrow_mut<B>(self, func: implFnOnce(&mut B)) -> Self
fntap_borrow_mut<B>(self, func: implFnOnce(&mut B)) -> Self
BorrowMut<B> of a value.Read moreSource§fntap_ref<R>(self, func: implFnOnce(&R)) -> Self
fntap_ref<R>(self, func: implFnOnce(&R)) -> Self
AsRef<R> view of a value.Read moreSource§fntap_ref_mut<R>(self, func: implFnOnce(&mut R)) -> Self
fntap_ref_mut<R>(self, func: implFnOnce(&mut R)) -> Self
AsMut<R> view of a value.Read moreSource§fntap_deref<T>(self, func: implFnOnce(&T)) -> Self
fntap_deref<T>(self, func: implFnOnce(&T)) -> Self
Deref::Target of a value.Read moreSource§fntap_deref_mut<T>(self, func: implFnOnce(&mut T)) -> Self
fntap_deref_mut<T>(self, func: implFnOnce(&mut T)) -> Self
Deref::Target of a value.Read moreSource§fntap_dbg(self, func: implFnOnce(&Self)) -> Self
fntap_dbg(self, func: implFnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fntap_mut_dbg(self, func: implFnOnce(&mut Self)) -> Self
fntap_mut_dbg(self, func: implFnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in releasebuilds.Source§fntap_borrow_dbg<B>(self, func: implFnOnce(&B)) -> Self
fntap_borrow_dbg<B>(self, func: implFnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in releasebuilds.Source§fntap_borrow_mut_dbg<B>(self, func: implFnOnce(&mut B)) -> Self
fntap_borrow_mut_dbg<B>(self, func: implFnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in releasebuilds.Source§fntap_ref_dbg<R>(self, func: implFnOnce(&R)) -> Self
fntap_ref_dbg<R>(self, func: implFnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in releasebuilds.Source§fntap_ref_mut_dbg<R>(self, func: implFnOnce(&mut R)) -> Self
fntap_ref_mut_dbg<R>(self, func: implFnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in releasebuilds.Source§fntap_deref_dbg<T>(self, func: implFnOnce(&T)) -> Self
fntap_deref_dbg<T>(self, func: implFnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in releasebuilds.