Skip to content

Commit

Permalink
improve error handling
Browse files Browse the repository at this point in the history
Signed-off-by: onur-ozkan <[email protected]>
  • Loading branch information
onur-ozkan committed Aug 30, 2023
1 parent 61bc5e7 commit d22ec06
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion mm2src/mm2_db/src/indexed_db/drivers/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 11 additions & 9 deletions mm2src/mm2_db/src/indexed_db/indexed_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,26 +805,28 @@ 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<web_sys::IdbFactory, String> {
pub fn get_idb_factory() -> Result<web_sys::IdbFactory, InitDbError> {
let global = js_sys::global();

let idb_factory = if let Some(window) = global.dyn_ref::<Window>() {
window.indexed_db()
} else if let Some(worker) = global.dyn_ref::<WorkerGlobalScope>() {
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::<Window>().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::<Window>().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))),
}
}

Expand Down
6 changes: 3 additions & 3 deletions mm2src/mm2_net/src/wasm_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<js_sys::Promise, String> {
pub fn compatible_fetch_with_request(js_request: &web_sys::Request) -> MmResult<js_sys::Promise, SlurpError> {
let global = js_sys::global();

if let Some(scope) = global.dyn_ref::<Window>() {
Expand All @@ -59,7 +59,7 @@ pub fn compatible_fetch_with_request(js_request: &web_sys::Request) -> Result<js
return Ok(scope.fetch_with_request(js_request));
}

Err(String::from("Unknown WASM environment."))
MmError::err(SlurpError::Internal("Unknown WASM environment.".to_string()))
}

pub struct FetchRequest {
Expand Down Expand Up @@ -180,7 +180,7 @@ impl FetchRequest {
.map_to_mm(|e| SlurpError::Internal(stringify_js_error(&e)))?;
}

let request_promise = compatible_fetch_with_request(&js_request).map_to_mm(SlurpError::Internal)?;
let request_promise = compatible_fetch_with_request(&js_request)?;

let future = JsFuture::from(request_promise);
let resp_value = future.await.map_to_mm(|e| SlurpError::Transport {
Expand Down

0 comments on commit d22ec06

Please sign in to comment.