use std::fmt;
#[derive(Debug)]
pub enum Error {
Io(std::io::Error),
Protocol(String),
Cdp { method: String, message: String },
Browser(String),
Shape(String),
Json(serde_json::Error),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io(e) => write!(f, "io: {e}"),
Error::Protocol(m) => write!(f, "websocket protocol: {m}"),
Error::Cdp { method, message } => write!(f, "cdp {method}: {message}"),
Error::Browser(m) => write!(f, "browser: {m}"),
Error::Shape(m) => write!(f, "unexpected response shape: {m}"),
Error::Json(e) => write!(f, "json: {e}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(e) => Some(e),
Error::Json(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::Io(e)
}
}
impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self {
Error::Json(e)
}
}
pub type Result<T> = std::result::Result<T, Error>;