use crate::{JSContext, JSContextImpl, JSValue, JSValueImpl};
use std::fmt;
use std::string::String;
#[derive(Debug, PartialEq, Eq)]
pub struct JSError {
pub message: Option<String>,
pub stack: Option<String>,
}
impl fmt::Display for JSError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
match (&self.message, &self.stack) {
(Some(msg), Some(stack)) => write!(f, "{}\n{}", msg, stack),
(Some(msg), None) => write!(f, "{}", msg),
(None, Some(stack)) => write!(f, "{}", stack),
(None, None) => write!(f, "Unknown JavaScript Error"),
}
}
}
impl std::error::Error for JSError {}
pub trait JSErrorFactory: JSContextImpl {
fn new_error(&self, name: &str, message: impl AsRef<str>, code: Option<&str>) -> Self::Value;
}
impl<C> JSContext<C>
where
C: JSContextImpl + JSErrorFactory,
C::Value: JSValueImpl,
{
pub fn new_error_value(
&self,
name: &str,
message: impl AsRef<str>,
code: Option<&str>,
) -> JSValue<C::Value> {
JSValue::from_raw(self, self.as_ref().new_error(name, message, code))
}
}