Skip to main content

eoka/
error.rs

1//! Error types for eoka
2
3use thiserror::Error;
4
5/// Result type for eoka operations
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Error type for eoka
9#[derive(Debug, Error)]
10pub enum Error {
11    /// Failed to launch Chrome
12    #[error("Failed to launch Chrome: {0}")]
13    Launch(String),
14
15    /// Transport error
16    #[error("Transport error: {context}")]
17    Transport {
18        context: String,
19        #[source]
20        source: Option<std::io::Error>,
21    },
22
23    /// CDP protocol error
24    #[error("CDP error in {method}: {message} (code {code})")]
25    Cdp {
26        method: String,
27        code: i64,
28        message: String,
29    },
30
31    /// CDP error without method context (for simple cases)
32    #[error("CDP error: {0}")]
33    CdpSimple(String),
34
35    /// Navigation error
36    #[error("Navigation error: {0}")]
37    Navigation(String),
38
39    /// Element not found in DOM
40    #[error("Element not found: {0}")]
41    ElementNotFound(String),
42
43    /// Element exists in DOM but is not visible/rendered
44    #[error("Element not visible: '{selector}' exists in DOM but is not rendered (hidden, display:none, or off-screen)")]
45    ElementNotVisible { selector: String },
46
47    /// Timeout
48    #[error("Timeout: {0}")]
49    Timeout(String),
50
51    /// Serialization error
52    #[error("Serialization error: {0}")]
53    Serialization(#[from] serde_json::Error),
54
55    /// Decode error (e.g., base64)
56    #[error("Decode error: {0}")]
57    Decode(String),
58
59    /// IO error
60    #[error("IO error: {0}")]
61    Io(#[from] std::io::Error),
62
63    /// Chrome not found
64    #[error("Chrome not found")]
65    ChromeNotFound,
66
67    /// Binary patching error
68    #[error("Patching error in {operation}: {message}")]
69    Patching { operation: String, message: String },
70
71    /// Retry exhausted
72    #[error("Retry exhausted after {attempts} attempts: {last_error}")]
73    RetryExhausted { attempts: u32, last_error: String },
74}
75
76impl Error {
77    /// Create a transport error with context
78    pub fn transport(context: impl Into<String>) -> Self {
79        Self::Transport {
80            context: context.into(),
81            source: None,
82        }
83    }
84
85    /// Create a transport error with IO source
86    pub fn transport_io(context: impl Into<String>, source: std::io::Error) -> Self {
87        Self::Transport {
88            context: context.into(),
89            source: Some(source),
90        }
91    }
92
93    /// Create a CDP error with full context
94    pub fn cdp(method: impl Into<String>, code: i64, message: impl Into<String>) -> Self {
95        Self::Cdp {
96            method: method.into(),
97            code,
98            message: message.into(),
99        }
100    }
101
102    /// Create a patching error
103    pub fn patching(operation: impl Into<String>, message: impl Into<String>) -> Self {
104        Self::Patching {
105            operation: operation.into(),
106            message: message.into(),
107        }
108    }
109}