eoka 0.12.0

Stealth browser automation for Rust. Puppeteer/Playwright alternative with anti-bot bypass.
Documentation
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//! Error types for eoka

use thiserror::Error;

pub(crate) mod classify;
mod code;
pub use code::ErrorCode;

/// Result type for eoka operations
pub type Result<T> = std::result::Result<T, Error>;

/// Error type for eoka.
///
/// `#[non_exhaustive]`: match with a `_ =>` arm so new variants are additive.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum Error {
    /// A failed high-level action, including whether its effect is uncertain.
    #[error("Action {code}: {source}")]
    Action {
        /// Stable machine-readable action error code.
        code: ErrorCode,
        /// Dispatch and verification evidence retained on failure.
        receipt: Box<crate::ActionReceipt>,
        #[source]
        /// Underlying cause of the failure.
        source: Box<Error>,
    },
    /// A failure eoka raised itself (usage, policy, state, timeouts, launch,
    /// navigation and decode failures), with a stable code. Branch on
    /// [`Error::code`]. Chrome's own protocol errors stay [`Error::Cdp`].
    #[error("{code}: {message}")]
    Coded {
        /// Stable machine-readable code.
        code: ErrorCode,
        /// Human-readable detail. Never contains caller-supplied secrets.
        message: String,
    },

    /// Transport error
    #[error(fmt = transport_fmt)]
    Transport {
        /// Description of what failed.
        context: String,
        /// Underlying IO error, if any.
        #[source]
        source: Option<std::io::Error>,
    },

    /// CDP protocol error. `method`/`code` carry command context when the error
    /// came from a command response, and are `None` for context-free failures.
    #[error(fmt = cdp_fmt)]
    Cdp {
        /// CDP command method that failed, if known.
        method: Option<String>,
        /// CDP error code, if known.
        code: Option<i64>,
        /// Error message.
        message: String,
    },

    /// Element not found in DOM
    #[error("Element not found: {0}")]
    ElementNotFound(String),

    /// Element exists in DOM but is not visible/rendered
    #[error("Element not visible: '{selector}' exists in DOM but is not rendered (hidden, display:none, or off-screen)")]
    ElementNotVisible {
        /// Selector that matched the hidden element.
        selector: String,
    },

    /// A URL could not be used. The message omits the URL, which may hold secrets.
    #[error("Invalid URL: {0}")]
    InvalidUrl(String),

    /// A form control settled on a different value than requested, for
    /// example because of a mask, `maxlength` or trimming. Values are not
    /// included: they may be passwords. Only their lengths are.
    #[error("Value mismatch for {selector}: the control holds {actual_chars} characters after filling {expected_chars}")]
    ValueMismatch {
        /// Description of the filled control.
        selector: String,
        /// Character count of the requested value.
        expected_chars: usize,
        /// Character count of the value read back from the DOM.
        actual_chars: usize,
    },

    /// Serialization error
    #[error("Serialization error: {0}")]
    Serialization(#[from] serde_json::Error),

    /// A checked JavaScript result exceeded its UTF-8 JSON byte limit.
    #[error("Evaluation result exceeds its JSON byte limit of {maximum_bytes}")]
    EvaluationTooLarge {
        /// Maximum encoded result size accepted by the caller.
        maximum_bytes: usize,
    },

    /// IO error
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    /// Chrome not found
    #[error("Chrome not found")]
    ChromeNotFound,

    /// Binary patching error
    #[error("Patching error in {operation}: {message}")]
    Patching {
        /// Patching operation that failed.
        operation: String,
        /// Error message.
        message: String,
    },

    /// Retry exhausted
    #[error("Retry exhausted after {attempts} attempts: {last_error}")]
    RetryExhausted {
        /// Number of attempts made before giving up.
        attempts: u32,
        /// Display text of the last error encountered.
        last_error: String,
        /// The last error encountered.
        #[source]
        source: Box<Error>,
    },
}

// `std::io::Error` and `serde_json::Error` aren't `Clone`, so we can't derive it.
// Reconstruct both best-effort (kind + message) so `Error` — and thus the
// broadcast `CdpMessage` — is cloneable.
impl Clone for Error {
    fn clone(&self) -> Self {
        use serde::de::Error as _;
        match self {
            Self::Action {
                code,
                receipt,
                source,
            } => Self::Action {
                code: *code,
                receipt: receipt.clone(),
                source: source.clone(),
            },
            Self::Coded { code, message } => Self::Coded {
                code: *code,
                message: message.clone(),
            },
            Self::Transport { context, source } => Self::Transport {
                context: context.clone(),
                source: source
                    .as_ref()
                    .map(|e| std::io::Error::new(e.kind(), e.to_string())),
            },
            Self::Cdp {
                method,
                code,
                message,
            } => Self::Cdp {
                method: method.clone(),
                code: *code,
                message: message.clone(),
            },
            Self::ElementNotFound(s) => Self::ElementNotFound(s.clone()),
            Self::ElementNotVisible { selector } => Self::ElementNotVisible {
                selector: selector.clone(),
            },
            Self::InvalidUrl(s) => Self::InvalidUrl(s.clone()),
            Self::ValueMismatch {
                selector,
                expected_chars,
                actual_chars,
            } => Self::ValueMismatch {
                selector: selector.clone(),
                expected_chars: *expected_chars,
                actual_chars: *actual_chars,
            },
            Self::Serialization(e) => Self::Serialization(serde_json::Error::custom(e.to_string())),
            Self::EvaluationTooLarge { maximum_bytes } => Self::EvaluationTooLarge {
                maximum_bytes: *maximum_bytes,
            },
            Self::Io(e) => Self::Io(std::io::Error::new(e.kind(), e.to_string())),
            Self::ChromeNotFound => Self::ChromeNotFound,
            Self::Patching { operation, message } => Self::Patching {
                operation: operation.clone(),
                message: message.clone(),
            },
            Self::RetryExhausted {
                attempts,
                last_error,
                source,
            } => Self::RetryExhausted {
                attempts: *attempts,
                last_error: last_error.clone(),
                source: source.clone(),
            },
        }
    }
}

/// Display for the `Cdp` variant, including method/code context when present.
fn cdp_fmt(
    method: &Option<String>,
    code: &Option<i64>,
    message: &str,
    f: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
    match (method, code) {
        (Some(m), Some(c)) => write!(f, "CDP error in {}: {} (code {})", m, message, c),
        (Some(m), None) => write!(f, "CDP error in {}: {}", m, message),
        _ => write!(f, "CDP error: {}", message),
    }
}

/// Display for the `Transport` variant, including the io error cause when present.
fn transport_fmt(
    context: &str,
    source: &Option<std::io::Error>,
    f: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
    match source {
        Some(err) => write!(f, "Transport error: {}: {}", context, err),
        None => write!(f, "Transport error: {}", context),
    }
}

impl Error {
    /// Decode a CDP JavaScript exception without exposing protocol implementation types.
    pub fn javascript_exception(details: &serde_json::Value) -> Self {
        match serde_json::from_value::<crate::cdp::types::ExceptionDetails>(details.clone()) {
            Ok(details) => Self::cdp_msg(details.message()),
            Err(_) => Self::cdp_msg("JavaScript exception details unavailable"),
        }
    }
    /// Create a transport error with context
    pub fn transport(context: impl Into<String>) -> Self {
        Self::Transport {
            context: context.into(),
            source: None,
        }
    }

    /// Create a transport error with IO source
    pub fn transport_io(context: impl Into<String>, source: std::io::Error) -> Self {
        Self::Transport {
            context: context.into(),
            source: Some(source),
        }
    }

    /// Create a CDP error with full command context.
    pub fn cdp(method: impl Into<String>, code: i64, message: impl Into<String>) -> Self {
        Self::Cdp {
            method: Some(method.into()),
            code: Some(code),
            message: message.into(),
        }
    }

    /// Create a CDP error without command context.
    pub fn cdp_msg(message: impl Into<String>) -> Self {
        Self::Cdp {
            method: None,
            code: None,
            message: message.into(),
        }
    }

    /// Create an error eoka raised itself, with a stable code.
    pub fn coded(code: ErrorCode, message: impl Into<String>) -> Self {
        Self::Coded {
            code,
            message: message.into(),
        }
    }

    /// Create an [`ErrorCode::Timeout`] error. This and [`Error::launch`] are
    /// shortcuts for the codes callers commonly raise; use [`Error::coded`]
    /// for any other code.
    pub fn timeout(message: impl Into<String>) -> Self {
        Self::coded(ErrorCode::Timeout, message)
    }

    /// Create an [`ErrorCode::LaunchFailed`] error.
    pub fn launch(message: impl Into<String>) -> Self {
        Self::coded(ErrorCode::LaunchFailed, message)
    }

    pub(crate) fn navigation(message: impl Into<String>) -> Self {
        Self::coded(ErrorCode::NavigationFailed, message)
    }

    pub(crate) fn invalid_argument(message: impl Into<String>) -> Self {
        Self::coded(ErrorCode::InvalidArgument, message)
    }

    pub(crate) fn decode(message: impl Into<String>) -> Self {
        Self::coded(ErrorCode::DecodeFailed, message)
    }

    pub(crate) fn input_state(message: impl Into<String>) -> Self {
        Self::coded(ErrorCode::InputState, message)
    }

    /// The stable code of this error, when it has one: every
    /// [`Error::Coded`] and [`Error::Action`], plus
    /// [`Error::ValueMismatch`], [`Error::InvalidUrl`],
    /// [`Error::EvaluationTooLarge`] and [`Error::ChromeNotFound`].
    pub fn code(&self) -> Option<ErrorCode> {
        match self {
            Self::Coded { code, .. } | Self::Action { code, .. } => Some(*code),
            Self::ValueMismatch { .. } => Some(ErrorCode::ValueMismatch),
            Self::InvalidUrl(_) => Some(ErrorCode::InvalidUrl),
            Self::EvaluationTooLarge { .. } => Some(ErrorCode::ResultTooLarge),
            Self::ChromeNotFound => Some(ErrorCode::ChromeNotFound),
            _ => None,
        }
    }

    /// Whether retrying the same call unchanged can succeed.
    ///
    /// Usage errors (invalid URLs, arguments or options, serialization,
    /// oversized results, a missing Chrome, invalid held-input state) are
    /// permanent; a [`Error::Coded`] error follows [`ErrorCode::is_transient`]. An action error is
    /// transient only when no input was sent and its code is transient, so a
    /// retry never repeats input that may have landed.
    pub fn is_retryable(&self) -> bool {
        match self {
            Self::Action { code, receipt, .. } => {
                receipt.dispatch == crate::DispatchState::NotSent && code.is_transient()
            }
            Self::Coded { code, .. } => code.is_transient(),
            Self::InvalidUrl(_)
            | Self::Serialization(_)
            | Self::EvaluationTooLarge { .. }
            | Self::ChromeNotFound
            | Self::ValueMismatch { .. }
            | Self::RetryExhausted { .. } => false,
            _ => true,
        }
    }

    /// Create a patching error
    pub fn patching(operation: impl Into<String>, message: impl Into<String>) -> Self {
        Self::Patching {
            operation: operation.into(),
            message: message.into(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::error::Error as StdError;

    #[test]
    fn test_transport_io_display_includes_context_and_cause() {
        let err = Error::transport_io(
            "Failed to connect",
            std::io::Error::new(std::io::ErrorKind::ConnectionRefused, "boom"),
        );
        let msg = format!("{}", err);
        // Both the context and the underlying io cause must appear.
        assert!(
            msg.contains("Failed to connect"),
            "missing context in: {msg}"
        );
        assert!(msg.contains("boom"), "missing io cause in: {msg}");
    }

    #[test]
    fn test_transport_io_exposes_source() {
        let err = Error::transport_io(
            "Failed to connect",
            std::io::Error::new(std::io::ErrorKind::ConnectionRefused, "boom"),
        );
        assert!(
            err.source().is_some(),
            "transport_io error should expose its io source"
        );
    }

    #[test]
    fn test_cdp_display_with_and_without_context() {
        let full = Error::cdp("DOM.resolveNode", -1, "boom");
        assert_eq!(
            format!("{full}"),
            "CDP error in DOM.resolveNode: boom (code -1)"
        );
        let simple = Error::cdp_msg("no value");
        assert_eq!(format!("{simple}"), "CDP error: no value");
    }

    #[test]
    fn test_error_codes_compare_with_their_string_form() {
        let error = Error::coded(ErrorCode::HistoryUnavailable, "no entry");
        assert_eq!(error.code(), Some(ErrorCode::HistoryUnavailable));
        assert!(ErrorCode::HistoryUnavailable == "history_unavailable");
        assert_eq!(error.to_string(), "history_unavailable: no entry");
        assert_eq!(
            serde_json::to_value(ErrorCode::ValueMismatch).unwrap(),
            serde_json::json!("value_mismatch")
        );
    }

    #[test]
    fn test_permanent_errors_are_not_retryable() {
        assert!(!Error::InvalidUrl("x".into()).is_retryable());
        assert!(!Error::coded(ErrorCode::AmbiguousTarget, "x").is_retryable());
        assert!(!Error::ChromeNotFound.is_retryable());
        assert!(Error::timeout("x").is_retryable());
        assert!(Error::launch("x").is_retryable());
        assert!(Error::decode("x").is_retryable());
        assert!(Error::coded(ErrorCode::NavigationFailed, "x").is_retryable());
        assert!(!Error::input_state("x").is_retryable());
        assert!(!Error::invalid_argument("x").is_retryable());
        assert_eq!(Error::timeout("x").code(), Some(ErrorCode::Timeout));
        assert_eq!(Error::timeout("x").to_string(), "timeout: x");
        assert_eq!(
            Error::ChromeNotFound.code(),
            Some(ErrorCode::ChromeNotFound)
        );
        assert_eq!(
            Error::InvalidUrl("x".into()).code(),
            Some(ErrorCode::InvalidUrl)
        );
        assert_eq!(
            Error::EvaluationTooLarge { maximum_bytes: 1 }.code(),
            Some(ErrorCode::ResultTooLarge)
        );
        assert!(Error::coded(ErrorCode::ActionabilityChanged, "x").is_retryable());
    }

    #[test]
    fn test_value_mismatch_hides_values() {
        let error = Error::ValueMismatch {
            selector: "#password".into(),
            expected_chars: 8,
            actual_chars: 6,
        };
        let text = error.to_string();
        assert!(text.contains("#password") && text.contains('8') && text.contains('6'));
    }

    #[test]
    fn test_transport_display_plain_message() {
        let err = Error::transport("plain msg");
        let msg = format!("{}", err);
        assert!(msg.contains("plain msg"), "missing message in: {msg}");
        // No source for the context-only constructor.
        assert!(err.source().is_none());
    }
}