Skip to content

Commit

Permalink
chore: better result error handling (#124)
Browse files Browse the repository at this point in the history
* Better result handling on windows

* and linux/macos

* Add changes file
  • Loading branch information
Ngo Iok Ui (Wu Yu Wei) authored Mar 21, 2021
1 parent 647c181 commit 485035f
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 38 deletions.
6 changes: 6 additions & 0 deletions .changes/unwrap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"wry": patch
---

Better result errror handling

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
47 changes: 32 additions & 15 deletions src/application/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
}
_ => {}
},
Expand All @@ -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) {
Expand Down
55 changes: 34 additions & 21 deletions src/application/gtkrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))")]
Expand Down
2 changes: 1 addition & 1 deletion src/webview/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/webview/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
1 change: 1 addition & 0 deletions src/webview/win/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 485035f

Please sign in to comment.