1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use base64::DecodeError;
use serde::Deserialize;

pub type WebDriverResult<T> = Result<T, WebDriverError>;

#[derive(Debug, Deserialize, Clone)]
pub struct WebDriverErrorValue {
    message: String,
    error: Option<String>,
    stacktrace: Option<String>,
    data: Option<serde_json::Value>,
}

#[derive(Debug, Deserialize, Clone)]
pub struct WebDriverErrorInfo {
    #[serde(skip)]
    pub status: u16,
    #[serde(default, rename(deserialize = "state"))]
    pub error: String,
    pub value: WebDriverErrorValue,
}

/// WebDriverError is the main error type
#[derive(Debug, Clone)]
pub enum WebDriverError {
    ConnectionError(RemoteConnectionError),
    NotFoundError(String),
    JsonError(String),
    DecodeError(String),
    IOError(String),
    RequestFailed(String),
    NotInSpec(WebDriverErrorInfo),
    ElementClickIntercepted(WebDriverErrorInfo),
    ElementNotInteractable(WebDriverErrorInfo),
    InsecureCertificate(WebDriverErrorInfo),
    InvalidArgument(WebDriverErrorInfo),
    InvalidCookieDomain(WebDriverErrorInfo),
    InvalidElementState(WebDriverErrorInfo),
    InvalidSelector(WebDriverErrorInfo),
    InvalidSessionId(WebDriverErrorInfo),
    JavascriptError(WebDriverErrorInfo),
    MoveTargetOutOfBounds(WebDriverErrorInfo),
    NoSuchAlert(WebDriverErrorInfo),
    NoSuchCookie(WebDriverErrorInfo),
    NoSuchElement(WebDriverErrorInfo),
    NoSuchFrame(WebDriverErrorInfo),
    NoSuchWindow(WebDriverErrorInfo),
    ScriptTimeout(WebDriverErrorInfo),
    SessionNotCreated(WebDriverErrorInfo),
    StaleElementReference(WebDriverErrorInfo),
    Timeout(WebDriverErrorInfo),
    UnableToSetCookie(WebDriverErrorInfo),
    UnableToCaptureScreen(WebDriverErrorInfo),
    UnexpectedAlertOpen(WebDriverErrorInfo),
    UnknownCommand(WebDriverErrorInfo),
    UnknownError(WebDriverErrorInfo),
    UnknownMethod(WebDriverErrorInfo),
    UnsupportedOperation(WebDriverErrorInfo),
}

impl WebDriverError {
    pub fn parse(status: u16, body: serde_json::Value) -> Self {
        let mut payload: WebDriverErrorInfo = match serde_json::from_value(body) {
            Ok(x) => x,
            Err(e) => {
                return WebDriverError::JsonError(e.to_string());
            }
        };
        payload.status = status;
        let mut error = payload.error.clone();
        if error.is_empty() {
            error = payload.value.error.clone().unwrap_or_default();
            if error.is_empty() {
                return WebDriverError::NotInSpec(payload);
            }
        }

        match error.as_str() {
            "element click intercepted" => WebDriverError::ElementClickIntercepted(payload),
            "element not interactable" => WebDriverError::ElementNotInteractable(payload),
            "insecure certificate" => WebDriverError::InsecureCertificate(payload),
            "invalid argument" => WebDriverError::InvalidArgument(payload),
            "invalid cookie domain" => WebDriverError::InvalidCookieDomain(payload),
            "invalid element state" => WebDriverError::InvalidElementState(payload),
            "invalid selector" => WebDriverError::InvalidSelector(payload),
            "invalid session id" => WebDriverError::InvalidSessionId(payload),
            "javascript error" => WebDriverError::JavascriptError(payload),
            "move target out of bounds" => WebDriverError::MoveTargetOutOfBounds(payload),
            "no such alert" => WebDriverError::NoSuchAlert(payload),
            "no such cookie" => WebDriverError::NoSuchCookie(payload),
            "no such element" => WebDriverError::NoSuchElement(payload),
            "no such frame" => WebDriverError::NoSuchFrame(payload),
            "no such window" => WebDriverError::NoSuchWindow(payload),
            "script timeout" => WebDriverError::ScriptTimeout(payload),
            "session not created" => WebDriverError::SessionNotCreated(payload),
            "stale element reference" => WebDriverError::StaleElementReference(payload),
            "timeout" => WebDriverError::Timeout(payload),
            "unable to set cookie" => WebDriverError::UnableToSetCookie(payload),
            "unable to capture screen" => WebDriverError::UnableToCaptureScreen(payload),
            "unexpected alert open" => WebDriverError::UnexpectedAlertOpen(payload),
            "unknown command" => WebDriverError::UnknownCommand(payload),
            "unknown error" => WebDriverError::UnknownError(payload),
            "unknown method" => WebDriverError::UnknownMethod(payload),
            "unsupported operation" => WebDriverError::UnsupportedOperation(payload),
            _ => WebDriverError::NotInSpec(payload),
        }
    }
}

impl From<serde_json::error::Error> for WebDriverError {
    fn from(value: serde_json::error::Error) -> Self {
        WebDriverError::JsonError(value.to_string())
    }
}

impl From<RemoteConnectionError> for WebDriverError {
    fn from(value: RemoteConnectionError) -> Self {
        WebDriverError::ConnectionError(value)
    }
}

impl From<DecodeError> for WebDriverError {
    fn from(value: DecodeError) -> Self {
        WebDriverError::DecodeError(value.to_string())
    }
}

impl From<std::io::Error> for WebDriverError {
    fn from(value: std::io::Error) -> Self {
        WebDriverError::IOError(value.to_string())
    }
}

#[derive(Debug, Clone)]
pub enum RemoteConnectionError {
    InvalidUrl(String),
    ClientError(String),
}

impl From<reqwest::header::InvalidHeaderValue> for RemoteConnectionError {
    fn from(value: reqwest::header::InvalidHeaderValue) -> Self {
        RemoteConnectionError::InvalidUrl(value.to_string())
    }
}

impl From<reqwest::Error> for RemoteConnectionError {
    fn from(value: reqwest::Error) -> Self {
        RemoteConnectionError::ClientError(value.to_string())
    }
}