//! Utility functions for the web backend. use wasm_bindgen::JsValue; #[derive(Debug)] pub struct JsError { value: JsValue, } impl std::fmt::Display for JsError { fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { write!(f, "JS exception: {:?}", self) } } impl std::error::Error for JsError {} pub trait JsResult { /// Converts a `JsValue` into a standard `Error`. fn warn_on_error(&self); fn into_js_result(self) -> Result; } impl JsResult for Result { #[inline] fn warn_on_error(&self) { if let Err(value) = self { log::warn!("Unexpected JavaScript error: {:?}", value); } } #[inline] fn into_js_result(self) -> Result { self.map_err(|value| JsError { value }) } }