use std::fmt;
#[derive(Debug)]
pub enum CdpError {
Protocol {
code: i64,
message: String,
data: Option<String>,
},
ConnectionClosed,
Timeout,
Serialization(serde_json::Error),
WebSocket(tokio_tungstenite::tungstenite::Error),
}
impl fmt::Display for CdpError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CdpError::Protocol {
code,
message,
data,
} => {
write!(f, "CDP protocol error {}: {}", code, message)?;
if let Some(d) = data {
write!(f, " ({})", d)?;
}
Ok(())
}
CdpError::ConnectionClosed => write!(f, "WebSocket connection closed"),
CdpError::Timeout => write!(f, "CDP command timed out"),
CdpError::Serialization(e) => write!(f, "Serialization error: {}", e),
CdpError::WebSocket(e) => write!(f, "WebSocket error: {}", e),
}
}
}
impl std::error::Error for CdpError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
CdpError::Serialization(e) => Some(e),
CdpError::WebSocket(e) => Some(e),
_ => None,
}
}
}
impl From<serde_json::Error> for CdpError {
fn from(e: serde_json::Error) -> Self {
CdpError::Serialization(e)
}
}
impl From<tokio_tungstenite::tungstenite::Error> for CdpError {
fn from(e: tokio_tungstenite::tungstenite::Error) -> Self {
CdpError::WebSocket(e)
}
}