1use std::fmt;
4
5#[derive(Debug)]
7pub enum CdpError {
8 WebSocket(Box<tokio_tungstenite::tungstenite::Error>),
10 Http(reqwest::Error),
12 Json(serde_json::Error),
14 Io(std::io::Error),
16 InvalidUrl(String),
18 Browser {
23 code: i64,
25 message: String,
27 data: Option<String>,
29 },
30 Protocol(String),
35 Timeout,
37 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
90pub type Result<T> = std::result::Result<T, CdpError>;