Struct boa_engine::error::JsError
source · 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 JsNativeError
s (for ideal
native errors) or opaque JsValue
s, 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
JsNativeError
s instead of plain JsValue
s. 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 const fn from_native(err: JsNativeError) -> Self
pub const 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 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::<ErrorObject>());
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:
message
MUST be aJsString
value.errors
(in the case ofAggregateError
s) MUST be anArray
object.
§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
AggregateError
s). 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().into());
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 PartialEq for JsError
impl PartialEq for JsError
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
impl StructuralPartialEq for JsError
Auto Trait Implementations§
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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
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<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.