viewpoint_cdp/error/
mod.rs

1//! CDP error types.
2
3use thiserror::Error;
4
5/// Errors that can occur during CDP communication.
6#[derive(Error, Debug)]
7pub enum CdpError {
8    /// WebSocket connection failed.
9    #[error("WebSocket connection failed: {0}")]
10    ConnectionFailed(String),
11
12    /// WebSocket connection was lost during operation.
13    #[error("WebSocket connection lost")]
14    ConnectionLost,
15
16    /// Failed to send a CDP message.
17    #[error("failed to send CDP message: {0}")]
18    SendFailed(String),
19
20    /// CDP protocol error returned by the browser.
21    #[error("CDP protocol error {code}: {message}")]
22    Protocol { code: i64, message: String },
23
24    /// JSON serialization/deserialization error.
25    #[error("JSON error: {0}")]
26    Json(#[from] serde_json::Error),
27
28    /// Response timeout.
29    #[error("response timeout after {0:?}")]
30    Timeout(std::time::Duration),
31
32    /// Invalid message ID in response.
33    #[error("invalid message ID: expected {expected}, got {got}")]
34    InvalidMessageId { expected: u64, got: u64 },
35
36    /// Failed to parse WebSocket URL.
37    #[error("invalid WebSocket URL: {0}")]
38    InvalidUrl(String),
39
40    /// Session not found.
41    #[error("session not found: {0}")]
42    SessionNotFound(String),
43
44    /// Browser process spawn failed.
45    #[error("failed to spawn browser process: {0}")]
46    SpawnFailed(String),
47
48    /// Failed to parse the debugging URL from browser output.
49    #[error("failed to get debugging URL from browser")]
50    NoDebuggingUrl,
51
52    /// Chromium executable not found.
53    #[error("Chromium not found. Set CHROMIUM_PATH environment variable or ensure Chromium is installed.")]
54    ChromiumNotFound,
55
56    /// Browser launch timeout.
57    #[error("browser launch timeout after {0:?}")]
58    LaunchTimeout(std::time::Duration),
59}
60
61impl From<tokio_tungstenite::tungstenite::Error> for CdpError {
62    fn from(err: tokio_tungstenite::tungstenite::Error) -> Self {
63        match err {
64            tokio_tungstenite::tungstenite::Error::ConnectionClosed
65            | tokio_tungstenite::tungstenite::Error::AlreadyClosed => Self::ConnectionLost,
66            other => Self::ConnectionFailed(other.to_string()),
67        }
68    }
69}
70
71#[cfg(test)]
72mod tests;