diff --git a/web/src/js_lib.rs b/web/src/js_lib.rs index 286cf67..e33ed87 100644 --- a/web/src/js_lib.rs +++ b/web/src/js_lib.rs @@ -12,13 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. use js_sys::Date; +use tracing::error; use wasm_bindgen::JsCast; -use web_sys::{window, Element, Storage}; +use web_sys::{window, Element, Storage, Window}; pub fn get_storage() -> Storage { - window() - .expect("No Window Present") - .local_storage() + get_window().local_storage() .expect("Failed to get storage") .expect("No storage available") } @@ -27,13 +26,16 @@ pub fn get_ms_timestamp() -> u32 { Date::new_0().get_milliseconds() } +pub fn get_window() -> Window { + window() + .expect("No window present") +} + pub fn get_element_by_id(id: &str) -> Result, Element> where E: JsCast, { - match window() - .expect("No window present") - .document() + match get_window().document() .expect("No document in window") .get_element_by_id(id) { @@ -41,3 +43,18 @@ where None => Ok(None), } } + +pub trait LogFailures { + fn swallow_and_log(self); +} + +impl LogFailures<(), E> for Result<(), E> +where + E: std::fmt::Debug, +{ + fn swallow_and_log(self) { + if let Err(e) = self { + error!(err = ?e, "Error: "); + } + } +}