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(
54        "Chromium not found. Set CHROMIUM_PATH environment variable or ensure Chromium is installed."
55    )]
56    ChromiumNotFound,
57
58    /// Browser launch timeout.
59    #[error("browser launch timeout after {0:?}")]
60    LaunchTimeout(std::time::Duration),
61
62    /// Connection timeout.
63    #[error("connection timeout after {0:?}")]
64    ConnectionTimeout(std::time::Duration),
65
66    /// Endpoint discovery failed.
67    #[error("failed to discover WebSocket endpoint from {url}: {reason}")]
68    EndpointDiscoveryFailed { url: String, reason: String },
69
70    /// Invalid endpoint URL.
71    #[error("invalid endpoint URL: {0}")]
72    InvalidEndpointUrl(String),
73
74    /// HTTP request failed.
75    #[error("HTTP request failed: {0}")]
76    HttpRequestFailed(String),
77}
78
79impl From<tokio_tungstenite::tungstenite::Error> for CdpError {
80    fn from(err: tokio_tungstenite::tungstenite::Error) -> Self {
81        match err {
82            tokio_tungstenite::tungstenite::Error::ConnectionClosed
83            | tokio_tungstenite::tungstenite::Error::AlreadyClosed => Self::ConnectionLost,
84            other => Self::ConnectionFailed(other.to_string()),
85        }
86    }
87}
88
89#[cfg(test)]
90mod tests;