From 485035f17d28560966b07b512935821814f0e951 Mon Sep 17 00:00:00 2001 From: "Ngo Iok Ui (Wu Yu Wei)" Date: Sun, 21 Mar 2021 20:52:58 +0800 Subject: [PATCH] chore: better result error handling (#124) * Better result handling on windows * and linux/macos * Add changes file --- .changes/unwrap.md | 6 +++++ Cargo.toml | 1 + src/application/general.rs | 47 +++++++++++++++++++++----------- src/application/gtkrs.rs | 55 +++++++++++++++++++++++--------------- src/lib.rs | 3 +++ src/webview/linux/mod.rs | 2 +- src/webview/macos/mod.rs | 2 +- src/webview/win/mod.rs | 1 + 8 files changed, 79 insertions(+), 38 deletions(-) create mode 100644 .changes/unwrap.md diff --git a/.changes/unwrap.md b/.changes/unwrap.md new file mode 100644 index 000000000..388c6c514 --- /dev/null +++ b/.changes/unwrap.md @@ -0,0 +1,6 @@ +--- +"wry": patch +--- + +Better result errror handling + diff --git a/Cargo.toml b/Cargo.toml index e6eda88b6..2448c5f6f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ protocol = [ ] [dependencies] libc = "0.2" +log = "0.4" once_cell = "1.7" serde = { version = "1.0", features = [ "derive" ] } serde_json = "1.0" diff --git a/src/application/general.rs b/src/application/general.rs index bbb8cc571..a7181eb09 100644 --- a/src/application/general.rs +++ b/src/application/general.rs @@ -161,7 +161,9 @@ impl App for InnerApplication { *control_flow = ControlFlow::Wait; for (_, w) in windows.iter() { - w.evaluate_script().unwrap(); + if let Err(e) = w.evaluate_script() { + log::error!("{}", e); + } } match event { Event::WindowEvent { event, window_id } => match event { @@ -173,7 +175,9 @@ impl App for InnerApplication { } } WindowEvent::Resized(_) => { - windows[&window_id].resize().unwrap(); + if let Err(e) = windows[&window_id].resize() { + log::error!("{}", e); + } } _ => {} }, @@ -186,19 +190,32 @@ impl App for InnerApplication { custom_protocol, ) => { let (window_attrs, webview_attrs) = attributes.split(); - let window = _create_window(&event_loop, window_attrs).unwrap(); - sender.send(window.id()).unwrap(); - let webview = _create_webview( - proxy.clone(), - window, - custom_protocol, - rpc_handler, - file_drop_handler, - webview_attrs, - ) - .unwrap(); - let id = webview.window().id(); - windows.insert(id, webview); + match _create_window(&event_loop, window_attrs) { + Ok(window) => { + if let Err(e) = sender.send(window.id()) { + log::error!("{}", e); + } + match _create_webview( + proxy.clone(), + window, + custom_protocol, + rpc_handler, + file_drop_handler, + webview_attrs, + ) { + Ok(webview) => { + let id = webview.window().id(); + windows.insert(id, webview); + } + Err(e) => { + log::error!("{}", e); + } + } + } + Err(e) => { + log::error!("{}", e); + } + } } Message::Window(id, window_message) => { if let Some(webview) = windows.get_mut(&id) { diff --git a/src/application/gtkrs.rs b/src/application/gtkrs.rs index 4a2a2e2f8..ed0d5bb83 100644 --- a/src/application/gtkrs.rs +++ b/src/application/gtkrs.rs @@ -158,27 +158,40 @@ impl App for InnerApplication { custom_protocol, ) => { let (window_attrs, webview_attrs) = attributes.split(); - let window = _create_window(&self.app, window_attrs).unwrap(); - sender.send(window.get_id()).unwrap(); - let webview = _create_webview( - proxy.clone(), - window, - custom_protocol, - rpc_handler, - file_drop_handler, - webview_attrs, - ) - .unwrap(); - let id = webview.window().get_id(); - let shared_webviews_ = shared_webviews_.clone(); - webview - .window() - .connect_delete_event(move |_window, _event| { - shared_webviews_.borrow_mut().remove(&id); - Inhibit(false) - }); - let mut webviews = shared_webviews.borrow_mut(); - webviews.insert(id, webview); + match _create_window(&self.app, window_attrs) { + Ok(window) => { + if let Err(e) = sender.send(window.get_id()) { + log::error!("{}", e); + } + match _create_webview( + proxy.clone(), + window, + custom_protocol, + rpc_handler, + file_drop_handler, + webview_attrs, + ) { + Ok(webview) => { + let id = webview.window().get_id(); + let shared_webviews_ = shared_webviews_.clone(); + webview + .window() + .connect_delete_event(move |_window, _event| { + shared_webviews_.borrow_mut().remove(&id); + Inhibit(false) + }); + let mut webviews = shared_webviews.borrow_mut(); + webviews.insert(id, webview); + } + Err(e) => { + log::error!("{}", e); + } + } + } + Err(e) => { + log::error!("{}", e); + } + } } Message::Window(id, window_message) => { if let Some(webview) = shared_webviews.borrow_mut().get_mut(&id) { diff --git a/src/lib.rs b/src/lib.rs index 8ab720bc7..ac59ed6b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -111,6 +111,9 @@ pub enum Error { #[cfg(target_os = "linux")] #[error(transparent)] GlibBoolError(#[from] glib::BoolError), + #[cfg(target_os = "linux")] + #[error("Fail to fetch security manager")] + MissingManager, #[error("Failed to initialize the script")] InitScriptError, #[error("Bad RPC request: {0} ((1))")] diff --git a/src/webview/linux/mod.rs b/src/webview/linux/mod.rs index 55368c6cf..5b434fc54 100644 --- a/src/webview/linux/mod.rs +++ b/src/webview/linux/mod.rs @@ -122,7 +122,7 @@ impl WV for InnerWebView { if let Some((name, handler)) = custom_protocol { context .get_security_manager() - .unwrap() + .ok_or(Error::MissingManager)? .register_uri_scheme_as_secure(&name); context.register_uri_scheme(&name.clone(), move |request| { if let Some(uri) = request.get_uri() { diff --git a/src/webview/macos/mod.rs b/src/webview/macos/mod.rs index c4cd24668..59e1ce352 100644 --- a/src/webview/macos/mod.rs +++ b/src/webview/macos/mod.rs @@ -320,7 +320,7 @@ impl NSString { let bytes: *const c_char = msg_send![self.0, UTF8String]; let len = msg_send![self.0, lengthOfBytesUsingEncoding: UTF8_ENCODING]; let bytes = slice::from_raw_parts(bytes as *const u8, len); - str::from_utf8(bytes).unwrap() + str::from_utf8_unchecked(bytes) } } } diff --git a/src/webview/win/mod.rs b/src/webview/win/mod.rs index 2489e8146..c230ef3a6 100644 --- a/src/webview/win/mod.rs +++ b/src/webview/win/mod.rs @@ -51,6 +51,7 @@ impl WV for InnerWebView { let user_data_path_provided: PathBuf; if user_data_path.is_some() { + // Safe to unwrap since this is already checked. user_data_path_provided = user_data_path.unwrap(); webview_builder = webview2::EnvironmentBuilder::new().with_user_data_folder(&user_data_path_provided);