fix error handling

This commit is contained in:
Toshit Chawda 2024-01-10 08:40:58 -08:00
parent 81e78c89bc
commit 3b4cd96614
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
3 changed files with 85 additions and 48 deletions

View file

@ -29,6 +29,10 @@ macro_rules! error {
($($t:tt)*) => (utils::console_error(&format_args!($($t)*).to_string()))
}
macro_rules! nerr {
($expr:expr) => (JsError::new($expr))
}
pub fn entries_of_object(obj: &Object) -> Vec<Vec<String>> {
js_sys::Object::entries(obj)
.to_vec()
@ -45,3 +49,25 @@ pub fn entries_of_object(obj: &Object) -> Vec<Vec<String>> {
})
.collect::<Vec<Vec<_>>>()
}
pub trait ReplaceErr {
type Ok;
fn replace_err(self, err: &str) -> Result<Self::Ok, JsError>;
}
impl<T, E> ReplaceErr for Result<T, E> {
type Ok = T;
fn replace_err(self, err: &str) -> Result<<Self as ReplaceErr>::Ok, JsError> {
self.map_err(|_| JsError::new(err))
}
}
impl<T> ReplaceErr for Option<T> {
type Ok = T;
fn replace_err(self, err: &str) -> Result<<Self as ReplaceErr>::Ok, JsError> {
self.ok_or_else(|| JsError::new(err))
}
}