1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
use thiserror::Error;
/// Boxed underlying error preserved on infrastructure-failure variants
/// (`Atspi`, `Process`, `Screenshot`) so callers can walk
/// [`std::error::Error::source`] and downcast to concrete types
/// (e.g. `zbus::Error`, `gstreamer::glib::Error`) when needed.
type BoxSource = Box<dyn std::error::Error + Send + Sync + 'static>;
/// Errors that can occur during a waydriver session.
#[derive(Debug, Error)]
pub enum Error {
#[error("element not found for selector: {xpath}")]
ElementNotFound { xpath: String },
#[error("selector matched {count} elements (expected exactly one): {xpath} — matched: {}", .matched.join(", "))]
AmbiguousSelector {
xpath: String,
count: usize,
/// Short descriptors of the matched elements (e.g.
/// `PushButton[name='Close']`), so the caller can tell *which* two
/// things collided without re-querying. Always populated when the
/// error is raised; may be empty for hand-constructed values.
matched: Vec<String>,
},
#[error("invalid selector '{xpath}': {reason}")]
InvalidSelector { xpath: String, reason: String },
#[error("element went stale during action (selector: {xpath}, bus: {bus}, path: {path})")]
ElementStale {
xpath: String,
bus: String,
path: String,
},
/// AT-SPI introspection / action failure.
///
/// `message` carries human-readable context (`"<operation>: <details>"`);
/// `source` carries the typed underlying error (commonly `zbus::Error`)
/// when one exists, so callers can downcast via
/// [`std::error::Error::source`]. Construct via [`Error::atspi`] /
/// [`Error::atspi_with`].
#[error("AT-SPI: {message}")]
Atspi {
message: String,
#[source]
source: Option<BoxSource>,
},
#[error("D-Bus: {0}")]
Zbus(#[from] zbus::Error),
#[error("IO: {0}")]
Io(#[from] std::io::Error),
#[error("timeout: {0}")]
Timeout(String),
/// The session's cancellation token was triggered while this
/// operation was in flight. Typically means `kill_session` was
/// called on an MCP session with in-flight tool calls — the tool
/// bails out of any auto-wait loop so kill can proceed without
/// waiting for the natural timeout.
#[error("operation cancelled")]
Cancelled,
/// Subprocess / IPC / mutex-poisoning failure. See [`Error::Atspi`] for
/// the field semantics; construct via [`Error::process`] /
/// [`Error::process_with`].
#[error("process: {message}")]
Process {
message: String,
#[source]
source: Option<BoxSource>,
},
/// Screenshot / video-capture failure. See [`Error::Atspi`] for the
/// field semantics; construct via [`Error::screenshot`] /
/// [`Error::screenshot_with`].
#[error("screenshot: {message}")]
Screenshot {
message: String,
#[source]
source: Option<BoxSource>,
},
/// Visual-locator (OCR) failure: model-load failures, multi-match
/// disambiguation, or downloads that failed. Construct via
/// [`Error::visual`].
#[error("visual: {0}")]
Visual(String),
}
impl Error {
/// AT-SPI failure with a free-form message and no underlying source.
pub fn atspi(message: impl Into<String>) -> Self {
Error::Atspi {
message: message.into(),
source: None,
}
}
/// AT-SPI failure caused by an underlying error. The `Display` of
/// `source` is appended to `operation` so the rendered message stays
/// `"AT-SPI: <operation>: <source>"`, and the typed source is preserved
/// for [`std::error::Error::source`].
pub fn atspi_with<E>(operation: impl std::fmt::Display, source: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
Error::Atspi {
message: format!("{operation}: {source}"),
source: Some(Box::new(source)),
}
}
/// Process / IPC failure with a free-form message and no source.
pub fn process(message: impl Into<String>) -> Self {
Error::Process {
message: message.into(),
source: None,
}
}
/// Process / IPC failure caused by an underlying error. See
/// [`Error::atspi_with`] for the message-formatting rules.
pub fn process_with<E>(operation: impl std::fmt::Display, source: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
Error::Process {
message: format!("{operation}: {source}"),
source: Some(Box::new(source)),
}
}
/// Screenshot / capture failure with a free-form message and no source.
pub fn screenshot(message: impl Into<String>) -> Self {
Error::Screenshot {
message: message.into(),
source: None,
}
}
/// Screenshot / capture failure caused by an underlying error. See
/// [`Error::atspi_with`] for the message-formatting rules.
pub fn screenshot_with<E>(operation: impl std::fmt::Display, source: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
Error::Screenshot {
message: format!("{operation}: {source}"),
source: Some(Box::new(source)),
}
}
/// Visual-locator failure with a free-form message.
pub fn visual(message: impl Into<String>) -> Self {
Error::Visual(message.into())
}
}
/// Convenience alias for `std::result::Result<T, waydriver::Error>`.
pub type Result<T> = std::result::Result<T, Error>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
assert_eq!(
Error::ElementNotFound {
xpath: "//PushButton[@name='OK']".into()
}
.to_string(),
"element not found for selector: //PushButton[@name='OK']"
);
assert_eq!(
Error::AmbiguousSelector {
xpath: "//PushButton".into(),
count: 2,
matched: vec![
"PushButton[name=\"OK\"]".into(),
"PushButton[name=\"Cancel\"]".into(),
],
}
.to_string(),
"selector matched 2 elements (expected exactly one): //PushButton — \
matched: PushButton[name=\"OK\"], PushButton[name=\"Cancel\"]"
);
assert_eq!(
Error::InvalidSelector {
xpath: "//[".into(),
reason: "unexpected token".into(),
}
.to_string(),
"invalid selector '//[': unexpected token"
);
assert_eq!(
Error::atspi("registry unavailable").to_string(),
"AT-SPI: registry unavailable"
);
assert_eq!(
Error::Timeout("socket did not appear".to_string()).to_string(),
"timeout: socket did not appear"
);
assert_eq!(
Error::process("dbus-launch failed").to_string(),
"process: dbus-launch failed"
);
assert_eq!(
Error::screenshot("capture failed").to_string(),
"screenshot: capture failed"
);
}
#[test]
fn test_error_from_io() {
let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "file missing");
let err = Error::from(io_err);
assert!(err.to_string().contains("IO:"));
assert!(err.to_string().contains("file missing"));
}
#[test]
fn with_constructor_appends_source_to_message() {
let io_err = std::io::Error::other("boom");
let err = Error::screenshot_with("CreateSession", io_err);
assert_eq!(err.to_string(), "screenshot: CreateSession: boom");
}
#[test]
fn with_constructor_preserves_typed_source() {
use std::error::Error as _;
let io_err = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "nope");
let err = Error::process_with("spawn", io_err);
let src = err.source().expect("source should be present");
let downcast = src
.downcast_ref::<std::io::Error>()
.expect("source should downcast to io::Error");
assert_eq!(downcast.kind(), std::io::ErrorKind::PermissionDenied);
}
#[test]
fn sourceless_constructor_has_no_source() {
use std::error::Error as _;
let err = Error::atspi("registry gone");
assert!(err.source().is_none());
}
}