Skip to content

Commit 559c534

Browse files
authored
Convert internal X11 errors to anyhow. (#982)
1 parent 1d432c3 commit 559c534

File tree

12 files changed

+285
-429
lines changed

12 files changed

+285
-429
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ This means that druid no longer requires cairo on macOS and uses Core Graphics i
144144
- Refactored DPI scaling. ([#904] by [@xStrom])
145145
- Added docs generation testing for all features. ([#942] by [@xStrom])
146146
- Renamed `BaseState` to `WidgetState` ([#969] by [@cmyr])
147+
- X11: Reworked error handling ([#982] by [@jneem])
147148

148149
### Outside News
149150

@@ -229,6 +230,7 @@ This means that druid no longer requires cairo on macOS and uses Core Graphics i
229230
[#969]: https://github.com/xi-editor/druid/pull/969
230231
[#970]: https://github.com/xi-editor/druid/pull/970
231232
[#980]: https://github.com/xi-editor/druid/pull/980
233+
[#982]: https://github.com/xi-editor/druid/pull/982
232234

233235
## [0.5.0] - 2020-04-01
234236

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

druid-shell/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ lazy_static = "1.0"
2626
time = "0.2.7"
2727
cfg-if = "0.1.10"
2828
instant = { version = "0.1", features = ["wasm-bindgen"] }
29+
anyhow = "1.0.31"
2930

3031
# Optional dependencies
3132
cairo-rs = { version = "0.8.1", default_features = false, optional = true }

druid-shell/src/application.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,7 @@ impl Application {
7979
return Err(Error::ApplicationAlreadyExists);
8080
}
8181
util::claim_main_thread();
82-
let platform_app = match platform::Application::new() {
83-
Ok(app) => app,
84-
Err(err) => return Err(Error::Platform(err)),
85-
};
82+
let platform_app = platform::Application::new()?;
8683
let state = Rc::new(RefCell::new(State { running: false }));
8784
let app = Application {
8885
platform_app,

druid-shell/src/error.rs

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//! Errors at the application shell level.
1616
1717
use std::fmt;
18+
use std::sync::Arc;
1819

1920
use crate::platform::error as platform;
2021

@@ -25,12 +26,10 @@ pub enum Error {
2526
ApplicationAlreadyExists,
2627
/// The window has already been destroyed.
2728
WindowDropped,
28-
/// Runtime borrow failure.
29-
BorrowError(BorrowError),
3029
/// Platform specific error.
3130
Platform(platform::Error),
3231
/// Other miscellaneous error.
33-
Other(&'static str),
32+
Other(Arc<anyhow::Error>),
3433
}
3534

3635
impl fmt::Display for Error {
@@ -39,64 +38,23 @@ impl fmt::Display for Error {
3938
Error::ApplicationAlreadyExists => {
4039
write!(f, "An application instance has already been created.")
4140
}
42-
Error::WindowDropped => write!(f, "The window has already been destroyed."),
43-
Error::BorrowError(err) => fmt::Display::fmt(err, f),
4441
Error::Platform(err) => fmt::Display::fmt(err, f),
42+
Error::WindowDropped => write!(f, "The window has already been destroyed."),
4543
Error::Other(s) => write!(f, "{}", s),
4644
}
4745
}
4846
}
4947

5048
impl std::error::Error for Error {}
5149

52-
impl From<platform::Error> for Error {
53-
fn from(src: platform::Error) -> Error {
54-
Error::Platform(src)
50+
impl From<anyhow::Error> for Error {
51+
fn from(src: anyhow::Error) -> Error {
52+
Error::Other(Arc::new(src))
5553
}
5654
}
5755

58-
/// Runtime borrow failure.
59-
#[derive(Debug, Clone)]
60-
pub struct BorrowError {
61-
location: &'static str,
62-
target: &'static str,
63-
mutable: bool,
64-
}
65-
66-
impl BorrowError {
67-
pub fn new(location: &'static str, target: &'static str, mutable: bool) -> BorrowError {
68-
BorrowError {
69-
location,
70-
target,
71-
mutable,
72-
}
73-
}
74-
}
75-
76-
impl fmt::Display for BorrowError {
77-
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
78-
if self.mutable {
79-
// Mutable borrow fails when any borrow exists
80-
write!(
81-
f,
82-
"{} was already borrowed in {}",
83-
self.target, self.location
84-
)
85-
} else {
86-
// Regular borrow fails when a mutable borrow exists
87-
write!(
88-
f,
89-
"{} was already mutably borrowed in {}",
90-
self.target, self.location
91-
)
92-
}
93-
}
94-
}
95-
96-
impl std::error::Error for BorrowError {}
97-
98-
impl From<BorrowError> for Error {
99-
fn from(src: BorrowError) -> Error {
100-
Error::BorrowError(src)
56+
impl From<platform::Error> for Error {
57+
fn from(src: platform::Error) -> Error {
58+
Error::Platform(src)
10159
}
10260
}

druid-shell/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
pub use kurbo;
2525
pub use piet_common as piet;
2626

27+
#[macro_use]
28+
mod util;
29+
2730
mod application;
2831
mod clipboard;
2932
mod common_util;
@@ -36,7 +39,6 @@ mod menu;
3639
mod mouse;
3740
mod platform;
3841
mod scale;
39-
mod util;
4042
mod window;
4143

4244
pub use application::{AppHandler, Application};

druid-shell/src/platform/gtk/dialog.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
1717
use std::ffi::OsString;
1818

19+
use anyhow::anyhow;
1920
use gtk::{FileChooserAction, FileChooserExt, FileFilter, NativeDialogExt, ResponseType, Window};
2021

2122
use crate::dialog::{FileDialogOptions, FileDialogType, FileSpec};
@@ -89,18 +90,18 @@ pub(crate) fn get_file_dialog_path(
8990
let result = match result {
9091
ResponseType::Accept => match dialog.get_filename() {
9192
Some(path) => Ok(path.into_os_string()),
92-
None => Err(Error::Other("No path received for filename")),
93+
None => Err(anyhow!("No path received for filename")),
9394
},
94-
ResponseType::Cancel => Err(Error::Other("Dialog was deleted")),
95+
ResponseType::Cancel => Err(anyhow!("Dialog was deleted")),
9596
_ => {
9697
log::warn!("Unhandled dialog result: {:?}", result);
97-
Err(Error::Other("Unhandled dialog result"))
98+
Err(anyhow!("Unhandled dialog result"))
9899
}
99100
};
100101

101102
// TODO properly handle errors into the Error type
102103

103104
dialog.destroy();
104105

105-
result
106+
Ok(result?)
106107
}

druid-shell/src/platform/gtk/window.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use std::slice;
2525
use std::sync::{Arc, Mutex, Weak};
2626
use std::time::Instant;
2727

28+
use anyhow::anyhow;
2829
use gdk::{EventKey, EventMask, ModifierType, ScrollDirection, WindowExt};
2930
use gio::ApplicationExt;
3031
use gtk::prelude::*;
@@ -694,9 +695,7 @@ impl WindowHandle {
694695
if let Some(state) = self.state.upgrade() {
695696
dialog::get_file_dialog_path(state.window.upcast_ref(), ty, options)
696697
} else {
697-
Err(ShellError::Other(
698-
"Cannot upgrade state from weak pointer to arc",
699-
))
698+
Err(anyhow!("Cannot upgrade state from weak pointer to arc").into())
700699
}
701700
}
702701
}

0 commit comments

Comments
 (0)