use thiserror::Error;
#[derive(Error, Debug)]
pub enum CdpError {
#[error("WebSocket connection failed: {0}")]
ConnectionFailed(String),
#[error("WebSocket connection lost")]
ConnectionLost,
#[error("failed to send CDP message: {0}")]
SendFailed(String),
#[error("CDP protocol error {code}: {message}")]
Protocol { code: i64, message: String },
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
#[error("response timeout after {0:?}")]
Timeout(std::time::Duration),
#[error("invalid message ID: expected {expected}, got {got}")]
InvalidMessageId { expected: u64, got: u64 },
#[error("invalid WebSocket URL: {0}")]
InvalidUrl(String),
#[error("session not found: {0}")]
SessionNotFound(String),
#[error("failed to spawn browser process: {0}")]
SpawnFailed(String),
#[error("failed to get debugging URL from browser")]
NoDebuggingUrl,
#[error(
"Chromium not found. Set CHROMIUM_PATH environment variable or ensure Chromium is installed."
)]
ChromiumNotFound,
#[error("browser launch timeout after {0:?}")]
LaunchTimeout(std::time::Duration),
#[error("connection timeout after {0:?}")]
ConnectionTimeout(std::time::Duration),
#[error("failed to discover WebSocket endpoint from {url}: {reason}")]
EndpointDiscoveryFailed { url: String, reason: String },
#[error("invalid endpoint URL: {0}")]
InvalidEndpointUrl(String),
#[error("HTTP request failed: {0}")]
HttpRequestFailed(String),
}
impl From<tokio_tungstenite::tungstenite::Error> for CdpError {
fn from(err: tokio_tungstenite::tungstenite::Error) -> Self {
match err {
tokio_tungstenite::tungstenite::Error::ConnectionClosed
| tokio_tungstenite::tungstenite::Error::AlreadyClosed => Self::ConnectionLost,
other => Self::ConnectionFailed(other.to_string()),
}
}
}
#[cfg(test)]
mod tests;