1use std::fmt;
2
3#[derive(Debug)]
5pub enum Error {
6 Io(std::io::Error),
8 Protocol(String),
10 Cdp { method: String, message: String },
12 Browser(String),
14 Shape(String),
16 Json(serde_json::Error),
18}
19
20impl fmt::Display for Error {
21 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22 match self {
23 Error::Io(e) => write!(f, "io: {e}"),
24 Error::Protocol(m) => write!(f, "websocket protocol: {m}"),
25 Error::Cdp { method, message } => write!(f, "cdp {method}: {message}"),
26 Error::Browser(m) => write!(f, "browser: {m}"),
27 Error::Shape(m) => write!(f, "unexpected response shape: {m}"),
28 Error::Json(e) => write!(f, "json: {e}"),
29 }
30 }
31}
32
33impl std::error::Error for Error {
34 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
35 match self {
36 Error::Io(e) => Some(e),
37 Error::Json(e) => Some(e),
38 _ => None,
39 }
40 }
41}
42
43impl From<std::io::Error> for Error {
44 fn from(e: std::io::Error) -> Self {
45 Error::Io(e)
46 }
47}
48
49impl From<serde_json::Error> for Error {
50 fn from(e: serde_json::Error) -> Self {
51 Error::Json(e)
52 }
53}
54
55pub type Result<T> = std::result::Result<T, Error>;