Skip to main content

proofsheet_core/
error.rs

1use std::fmt;
2
3/// Everything that can go wrong inside the core.
4#[derive(Debug)]
5pub enum Error {
6    /// Underlying socket / process I/O failure.
7    Io(std::io::Error),
8    /// The WebSocket upgrade or framing went wrong.
9    Protocol(String),
10    /// Chrome accepted the command but answered with an error.
11    Cdp { method: String, message: String },
12    /// Chrome could not be found, launched, or attached to.
13    Browser(String),
14    /// A response did not have the shape we required.
15    Shape(String),
16    /// Serialization / deserialization failure.
17    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>;