Skip to main content

cdp_driver/
error.rs

1//! The crate's error type and [`Result`] alias.
2
3use std::fmt;
4
5/// Errors returned by every fallible operation in this crate.
6#[derive(Debug)]
7pub enum CdpError {
8    /// The CDP WebSocket connection failed or dropped.
9    WebSocket(Box<tokio_tungstenite::tungstenite::Error>),
10    /// A request to Chrome's HTTP endpoint (`/json/*`) failed.
11    Http(reqwest::Error),
12    /// A CDP payload failed to (de)serialize.
13    Json(serde_json::Error),
14    /// A filesystem operation (e.g. writing a screenshot) failed.
15    Io(std::io::Error),
16    /// A URL supplied by the caller or returned by Chrome was malformed or missing.
17    InvalidUrl(String),
18    /// Chrome rejected a command. `code` is CDP's own numeric code (`-32601` for a
19    /// method the browser does not know, `-32602` for bad parameters), `message` is
20    /// what Chrome called the failure, and `data` is the extra detail some errors
21    /// carry. Branch on `code` rather than on the wording of `message`.
22    Browser {
23        /// CDP's numeric error code.
24        code: i64,
25        /// Chrome's description of what went wrong.
26        message: String,
27        /// Extra detail, present on some errors only.
28        data: Option<String>,
29    },
30    /// This crate could not make sense of an otherwise successful exchange: a
31    /// response of an unexpected shape, a command the transport could not send, or
32    /// an operation the browser reported as failed through its result rather than
33    /// through an error frame.
34    Protocol(String),
35    /// A command or event wait exceeded its configured timeout.
36    Timeout,
37    /// No page target was available to connect to (e.g. `list_targets` returned none).
38    NoTarget,
39}
40
41impl fmt::Display for CdpError {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        match self {
44            CdpError::WebSocket(e) => write!(f, "WebSocket error: {e}"),
45            CdpError::Http(e) => write!(f, "HTTP error: {e}"),
46            CdpError::Json(e) => write!(f, "JSON error: {e}"),
47            CdpError::Io(e) => write!(f, "IO error: {e}"),
48            CdpError::InvalidUrl(s) => write!(f, "Invalid URL: {s}"),
49            CdpError::Browser {
50                code,
51                message,
52                data: Some(detail),
53            } => write!(f, "Browser error {code}: {message} ({detail})"),
54            CdpError::Browser { code, message, .. } => {
55                write!(f, "Browser error {code}: {message}")
56            }
57            CdpError::Protocol(s) => write!(f, "Protocol error: {s}"),
58            CdpError::Timeout => write!(f, "Operation timed out"),
59            CdpError::NoTarget => write!(f, "No page target available"),
60        }
61    }
62}
63
64impl std::error::Error for CdpError {}
65
66impl From<tokio_tungstenite::tungstenite::Error> for CdpError {
67    fn from(e: tokio_tungstenite::tungstenite::Error) -> Self {
68        CdpError::WebSocket(Box::new(e))
69    }
70}
71
72impl From<reqwest::Error> for CdpError {
73    fn from(e: reqwest::Error) -> Self {
74        CdpError::Http(e)
75    }
76}
77
78impl From<serde_json::Error> for CdpError {
79    fn from(e: serde_json::Error) -> Self {
80        CdpError::Json(e)
81    }
82}
83
84impl From<std::io::Error> for CdpError {
85    fn from(e: std::io::Error) -> Self {
86        CdpError::Io(e)
87    }
88}
89
90/// Convenience alias for `std::result::Result<T, CdpError>`.
91pub type Result<T> = std::result::Result<T, CdpError>;