Movatterモバイル変換


[0]ホーム

URL:


Docs.rs

JsError

StructJsError 

Source
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

Source

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());
Source

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());
Source

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());
Source

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>());
Source

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 aJsString value.
  • errors (in the case ofAggregateErrors)MUST be anArray object.
§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!");
Source

pub const fnas_opaque(&self) ->Option<&JsValue>

Gets the innerJsValue if the error is an opaque error,orNone otherwise.

§Examples
leterror: JsError = JsNativeError::reference()    .with_message("variable not found!")    .into();assert!(error.as_opaque().is_none());leterror = JsError::from_opaque(256u32.into());assert!(error.as_opaque().is_some());
Source

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());
Source

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§

implClone forJsError

Source§

fnclone(&self) ->JsError

Returns a duplicate of the value.Read more
1.0.0 ·Source§

fnclone_from(&mut self, source: &Self)

Performs copy-assignment fromsource.Read more
Source§

implDebug forJsError

Source§

fnfmt(&self, f: &mutFormatter<'_>) ->Result

Formats the value using the given formatter.Read more
Source§

implDisplay forJsError

Source§

fnfmt(&self, f: &mutFormatter<'_>) ->Result

Formats the value using the given formatter.Read more
Source§

implError forJsError

Source§

fnsource(&self) ->Option<&(dynError + 'static)>

Returns the lower-level source of this error, if any.Read more
1.0.0 ·Source§

fndescription(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 ·Source§

fncause(&self) ->Option<&dynError>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fnprovide<'a>(&'a self, request: &mutRequest<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports.Read more
Source§

implFinalize forJsError

Source§

fnfinalize(&self)

Cleanup logic for a type.
Source§

implFrom<Error> forJsError

Source§

fnfrom(err:Error) -> Self

Converts to this type from the input type.
Source§

implFrom<IcuError> forJsError

Source§

fnfrom(value:IcuError) -> Self

Converts to this type from the input type.
Source§

implFrom<JsNativeError> forJsError

Source§

fnfrom(error:JsNativeError) -> Self

Converts to this type from the input type.
Source§

implFrom<TemporalError> forJsError

Source§

fnfrom(value:TemporalError) -> Self

Converts to this type from the input type.
Source§

implPartialEq forJsError

Source§

fneq(&self, other: &Self) ->bool

The backtrace information is ignored.

1.0.0 ·Source§

fnne(&self, other:&Rhs) ->bool

Tests for!=. The default implementation is almost always sufficient,and should not be overridden without very good reason.
Source§

implTrace forJsError

Source§

unsafe fntrace(&self, tracer: &mut Tracer)

Marks all containedGcs.Read more
Source§

unsafe fntrace_non_roots(&self)

Trace handles located in GC heap, and mark them as non root.Read more
Source§

fnrun_finalizer(&self)

RunsFinalize::finalize on this object and allcontained subobjects.
Source§

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>Any for T
where T: 'static + ?Sized,

Source§

fntype_id(&self) ->TypeId

Gets theTypeId ofself.Read more
Source§

impl<T>Borrow<T> for T
where T: ?Sized,

Source§

fnborrow(&self) ->&T

Immutably borrows from an owned value.Read more
Source§

impl<T>BorrowMut<T> for T
where T: ?Sized,

Source§

fnborrow_mut(&mut self) ->&mut T

Mutably borrows from an owned value.Read more
Source§

impl<T>CloneToUninit for T
where T:Clone,

Source§

unsafe fnclone_to_uninit(&self, dest:*mutu8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment fromself todest.Read more
Source§

impl<T>Conv for T

Source§

fnconv<T>(self) -> T
where Self:Into<T>,

Convertsself intoT usingInto<T>.Read more
Source§

impl<Q, K>Equivalent<K> for Q
where Q:Eq + ?Sized, K:Borrow<Q> + ?Sized,

Source§

fnequivalent(&self, key:&K) ->bool

Compare self tokey and returntrue if they are equal.
Source§

impl<Q, K>Equivalent<K> for Q
where Q:Eq + ?Sized, K:Borrow<Q> + ?Sized,

Source§

fnequivalent(&self, key:&K) ->bool

Checks if this value is equivalent to the given key.Read more
Source§

impl<T>From<T> for T

Source§

fnfrom(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U>Into<U> for T
where U:From<T>,

Source§

fninto(self) -> U

CallsU::from(self).

That is, this conversion is whatever the implementation ofFrom<T> for U chooses to do.

Source§

impl<T>IntoEither for T

Source§

fninto_either(self, into_left:bool) ->Either<Self, Self>

Convertsself into aLeft variant ofEither<Self, Self>ifinto_left istrue.Convertsself into aRight variant ofEither<Self, Self>otherwise.Read more
Source§

fninto_either_with<F>(self, into_left: F) ->Either<Self, Self>
where F:FnOnce(&Self) ->bool,

Convertsself into aLeft variant ofEither<Self, Self>ifinto_left(&self) returnstrue.Convertsself into aRight variant ofEither<Self, Self>otherwise.Read more
Source§

impl<T>Pipe for T
where T: ?Sized,

Source§

fnpipe<R>(self, func: implFnOnce(Self) -> R) -> R
where Self:Sized,

Pipes by value. This is generally the method you want to use.Read more
Source§

fnpipe_ref<'a, R>(&'a self, func: implFnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrowsself and passes that borrow into the pipe function.Read more
Source§

fnpipe_ref_mut<'a, R>(&'a mut self, func: implFnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrowsself and passes that borrow into the pipe function.Read more
Source§

fnpipe_borrow<'a, B, R>(&'a self, func: implFnOnce(&'a B) -> R) -> R
where Self:Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrowsself, then passesself.borrow() into the pipe function.Read more
Source§

fnpipe_borrow_mut<'a, B, R>( &'a mut self, func: implFnOnce(&'a mut B) -> R,) -> R
where Self:BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrowsself, then passesself.borrow_mut() into the pipefunction.Read more
Source§

fnpipe_as_ref<'a, U, R>(&'a self, func: implFnOnce(&'a U) -> R) -> R
where Self:AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrowsself, 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
where Self:AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrowsself, then passesself.as_mut() into the pipefunction.
Source§

fnpipe_deref<'a, T, R>(&'a self, func: implFnOnce(&'a T) -> R) -> R
where Self:Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrowsself, then passesself.deref() into the pipe function.
Source§

fnpipe_deref_mut<'a, T, R>( &'a mut self, func: implFnOnce(&'a mut T) -> R,) -> R
where Self:DerefMut<Target = T> +Deref, T: 'a + ?Sized, R: 'a,

Mutably borrowsself, then passesself.deref_mut() into the pipefunction.
Source§

impl<T>Tap for T

Source§

fntap(self, func: implFnOnce(&Self)) -> Self

Immutable access to a value.Read more
Source§

fntap_mut(self, func: implFnOnce(&mut Self)) -> Self

Mutable access to a value.Read more
Source§

fntap_borrow<B>(self, func: implFnOnce(&B)) -> Self
where Self:Borrow<B>, B: ?Sized,

Immutable access to theBorrow<B> of a value.Read more
Source§

fntap_borrow_mut<B>(self, func: implFnOnce(&mut B)) -> Self
where Self:BorrowMut<B>, B: ?Sized,

Mutable access to theBorrowMut<B> of a value.Read more
Source§

fntap_ref<R>(self, func: implFnOnce(&R)) -> Self
where Self:AsRef<R>, R: ?Sized,

Immutable access to theAsRef<R> view of a value.Read more
Source§

fntap_ref_mut<R>(self, func: implFnOnce(&mut R)) -> Self
where Self:AsMut<R>, R: ?Sized,

Mutable access to theAsMut<R> view of a value.Read more
Source§

fntap_deref<T>(self, func: implFnOnce(&T)) -> Self
where Self:Deref<Target = T>, T: ?Sized,

Immutable access to theDeref::Target of a value.Read more
Source§

fntap_deref_mut<T>(self, func: implFnOnce(&mut T)) -> Self
where Self:DerefMut<Target = T> +Deref, T: ?Sized,

Mutable access to theDeref::Target of a value.Read more
Source§

fntap_dbg(self, func: implFnOnce(&Self)) -> Self

Calls.tap() only in debug builds, and is erased in release builds.
Source§

fntap_mut_dbg(self, func: implFnOnce(&mut Self)) -> Self

Calls.tap_mut() only in debug builds, and is erased in releasebuilds.
Source§

fntap_borrow_dbg<B>(self, func: implFnOnce(&B)) -> Self
where Self:Borrow<B>, B: ?Sized,

Calls.tap_borrow() only in debug builds, and is erased in releasebuilds.
Source§

fntap_borrow_mut_dbg<B>(self, func: implFnOnce(&mut B)) -> Self
where Self:BorrowMut<B>, B: ?Sized,

Calls.tap_borrow_mut() only in debug builds, and is erased in releasebuilds.
Source§

fntap_ref_dbg<R>(self, func: implFnOnce(&R)) -> Self
where Self:AsRef<R>, R: ?Sized,

Calls.tap_ref() only in debug builds, and is erased in releasebuilds.
Source§

fntap_ref_mut_dbg<R>(self, func: implFnOnce(&mut R)) -> Self
where Self:AsMut<R>, R: ?Sized,

Calls.tap_ref_mut() only in debug builds, and is erased in releasebuilds.
Source§

fntap_deref_dbg<T>(self, func: implFnOnce(&T)) -> Self
where Self:Deref<Target = T>, T: ?Sized,

Calls.tap_deref() only in debug builds, and is erased in releasebuilds.
Source§

fntap_deref_mut_dbg<T>(self, func: implFnOnce(&mut T)) -> Self
where Self:DerefMut<Target = T> +Deref, T: ?Sized,

Calls.tap_deref_mut() only in debug builds, and is erased in releasebuilds.
Source§

impl<T>ToOwned for T
where T:Clone,

Source§

typeOwned = T

The resulting type after obtaining ownership.
Source§

fnto_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning.Read more
Source§

fnclone_into(&self, target:&mut T)

Uses borrowed data to replace owned data, usually by cloning.Read more
Source§

impl<T>ToString for T
where T:Display + ?Sized,

Source§

fnto_string(&self) ->String

Converts the given value to aString.Read more
Source§

impl<T>TryConv for T

Source§

fntry_conv<T>(self) ->Result<T, Self::Error>
where Self:TryInto<T>,

Attempts to convertself intoT usingTryInto<T>.Read more
Source§

impl<T, U>TryFrom<U> for T
where U:Into<T>,

Source§

typeError =Infallible

The type returned in the event of a conversion error.
Source§

fntry_from(value: U) ->Result<T, <T asTryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U>TryInto<U> for T
where U:TryFrom<T>,

Source§

typeError = <U asTryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fntry_into(self) ->Result<U, <U asTryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T>VZip<V> for T
where V:MultiLane<T>,

Source§

fnvzip(self) -> V

Source§

impl<T>ErasedDestructor for T
where T: 'static,


[8]ページ先頭

©2009-2025 Movatter.jp