From d22ec069716754ca1e69a51ce2a6233250033dcd Mon Sep 17 00:00:00 2001 From: onur-ozkan Date: Wed, 30 Aug 2023 11:21:48 +0300 Subject: [PATCH] improve error handling Signed-off-by: onur-ozkan --- .../mm2_db/src/indexed_db/drivers/builder.rs | 2 +- mm2src/mm2_db/src/indexed_db/indexed_db.rs | 20 ++++++++++--------- mm2src/mm2_net/src/wasm_http.rs | 6 +++--- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/mm2src/mm2_db/src/indexed_db/drivers/builder.rs b/mm2src/mm2_db/src/indexed_db/drivers/builder.rs index 25080ddc3bf..8fc411c8cbc 100644 --- a/mm2src/mm2_db/src/indexed_db/drivers/builder.rs +++ b/mm2src/mm2_db/src/indexed_db/drivers/builder.rs @@ -74,7 +74,7 @@ impl IdbDatabaseBuilder { let (table_names, on_upgrade_needed_handlers) = Self::tables_into_parts(self.tables)?; info!("Open '{}' database with tables: {:?}", self.db_name, table_names); - let indexed_db = get_idb_factory().map_err(InitDbError::NotSupported)?; + let indexed_db = get_idb_factory()?; let db_request = match indexed_db.open_with_u32(&self.db_name, self.db_version) { Ok(r) => r, diff --git a/mm2src/mm2_db/src/indexed_db/indexed_db.rs b/mm2src/mm2_db/src/indexed_db/indexed_db.rs index 373be72be3f..853e431e412 100644 --- a/mm2src/mm2_db/src/indexed_db/indexed_db.rs +++ b/mm2src/mm2_db/src/indexed_db/indexed_db.rs @@ -805,7 +805,7 @@ fn open_cursor( /// Detects the current execution environment (window or worker) and follows the appropriate way /// of getting `web_sys::IdbFactory` instance. -pub fn get_idb_factory() -> Result { +pub fn get_idb_factory() -> Result { let global = js_sys::global(); let idb_factory = if let Some(window) = global.dyn_ref::() { @@ -813,18 +813,20 @@ pub fn get_idb_factory() -> Result { } else if let Some(worker) = global.dyn_ref::() { worker.indexed_db() } else { - return Err(String::from("Unknown WASM environment.")); + return Err(InitDbError::NotSupported("Unknown WASM environment.".to_string())); }; match idb_factory { Ok(Some(db)) => Ok(db), - Ok(None) => Err(if global.dyn_ref::().is_some() { - "IndexedDB not supported in window context" - } else { - "IndexedDB not supported in worker context" - } - .to_string()), - Err(e) => Err(stringify_js_error(&e)), + Ok(None) => Err(InitDbError::NotSupported( + if global.dyn_ref::().is_some() { + "IndexedDB not supported in window context" + } else { + "IndexedDB not supported in worker context" + } + .to_string(), + )), + Err(e) => Err(InitDbError::NotSupported(stringify_js_error(&e))), } } diff --git a/mm2src/mm2_net/src/wasm_http.rs b/mm2src/mm2_net/src/wasm_http.rs index 10245a60f92..9c07a75c77d 100644 --- a/mm2src/mm2_net/src/wasm_http.rs +++ b/mm2src/mm2_net/src/wasm_http.rs @@ -48,7 +48,7 @@ pub async fn slurp_post_json(url: &str, body: String) -> SlurpResult { /// This function is a wrapper around the `fetch_with_request`, providing compatibility across /// different execution environments, such as window and worker. -pub fn compatible_fetch_with_request(js_request: &web_sys::Request) -> Result { +pub fn compatible_fetch_with_request(js_request: &web_sys::Request) -> MmResult { let global = js_sys::global(); if let Some(scope) = global.dyn_ref::() { @@ -59,7 +59,7 @@ pub fn compatible_fetch_with_request(js_request: &web_sys::Request) -> Result