--- a/testing/webdriver/src/error.rs
+++ b/testing/webdriver/src/error.rs
@@ -136,18 +136,18 @@ pub enum ErrorStatus {
UnknownPath,
/// Indicates that a [command] that should have executed properly is not
/// currently supported.
UnsupportedOperation,
}
-
impl ErrorStatus {
+ /// Returns the string serialisation of the error type.
pub fn error_code(&self) -> &'static str {
use self::ErrorStatus::*;
match *self {
ElementClickIntercepted => "element click intercepted",
ElementNotInteractable => "element not interactable",
ElementNotSelectable => "element not selectable",
InsecureCertificate => "insecure certificate",
InvalidArgument => "invalid argument",
@@ -173,16 +173,17 @@ impl ErrorStatus {
UnknownCommand |
UnknownError => "unknown error",
UnknownMethod => "unknown method",
UnknownPath => "unknown command",
UnsupportedOperation => "unsupported operation",
}
}
+ /// Returns the correct HTTP status code associated with the error type.
pub fn http_status(&self) -> StatusCode {
use self::ErrorStatus::*;
use self::StatusCode::*;
match *self {
ElementClickIntercepted => BadRequest,
ElementNotInteractable => BadRequest,
ElementNotSelectable => BadRequest,
InsecureCertificate => BadRequest,
@@ -210,16 +211,52 @@ impl ErrorStatus {
UnknownError => InternalServerError,
UnknownMethod => MethodNotAllowed,
UnknownPath => NotFound,
UnsupportedOperation => InternalServerError,
}
}
}
+/// Deserialises error type from string.
+impl From<String> for ErrorStatus {
+ fn from(s: String) -> ErrorStatus {
+ use self::ErrorStatus::*;
+ match &*s {
+ "element click intercepted" => ElementClickIntercepted,
+ "element not interactable" | "element not visible" => ElementNotInteractable,
+ "element not selectable" => ElementNotSelectable,
+ "insecure certificate" => InsecureCertificate,
+ "invalid argument" => InvalidArgument,
+ "invalid cookie domain" => InvalidCookieDomain,
+ "invalid coordinates" | "invalid element coordinates" => InvalidCoordinates,
+ "invalid element state" => InvalidElementState,
+ "invalid selector" => InvalidSelector,
+ "invalid session id" => InvalidSessionId,
+ "javascript error" => JavascriptError,
+ "move target out of bounds" => MoveTargetOutOfBounds,
+ "no such alert" => NoSuchAlert,
+ "no such element" => NoSuchElement,
+ "no such frame" => NoSuchFrame,
+ "no such window" => NoSuchWindow,
+ "script timeout" => ScriptTimeout,
+ "session not created" => SessionNotCreated,
+ "stale element reference" => StaleElementReference,
+ "timeout" => Timeout,
+ "unable to capture screen" => UnableToCaptureScreen,
+ "unable to set cookie" => UnableToSetCookie,
+ "unexpected alert open" => UnexpectedAlertOpen,
+ "unknown command" => UnknownCommand,
+ "unknown error" => UnknownError,
+ "unsupported operation" => UnsupportedOperation,
+ _ => UnknownError,
+ }
+ }
+}
+
pub type WebDriverResult<T> = Result<T, WebDriverError>;
#[derive(Debug)]
pub struct WebDriverError {
pub error: ErrorStatus,
pub message: Cow<'static, str>,
pub stack: Cow<'static, str>,
pub delete_session: bool,