author Andreas Tolfsen <ato@sny.no>
Mon, 23 Oct 2017 21:46:44 +0100
changeset 685721 135d220e21783e0f4d892687594aedd672ba13da
parent 659355 bcd74edc5749d59828144f1414411022f1e4f5b5
child 689654 fd576f3be7518d6b47c62557e18826c0705beef3
child 690131 ee7953c40310d49f42efc368b2f3bded548ede4c
permissions -rw-r--r--
Bug 1411026 - Provide String type conversion for ErrorStatus. r=whimboo This allows us to construct an ErrorStatus variant from a String. This is useful if the remote end implementation uses the string codes for transport. The new From<String> trait for ErrorStatus is a reverse lookup of ErrorStatus::error_code. It currently handles two cases of Selenium errors, which are "element not visible" and "invalid element coordinates". Both these have been deprecated in the WebDriver standard and we need to figure out a deprecation strategy for them. MozReview-Commit-ID: 48MAVNQoiKy

use backtrace::Backtrace;
use hyper::status::StatusCode;
use rustc_serialize::base64::FromBase64Error;
use rustc_serialize::json::{DecoderError, Json, ParserError, ToJson};
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::convert::From;
use std::error::Error;
use std::fmt;
use std::io;

#[derive(Debug, PartialEq)]
pub enum ErrorStatus {
    /// The [`ElementClick`] command could not be completed because the
    /// [element] receiving the events is obscuring the element that was
    /// requested clicked.
    /// [`ElementClick`]:
    /// ../command/enum.WebDriverCommand.html#variant.ElementClick
    /// [element]: ../common/struct.WebElement.html

    /// A [command] could not be completed because the element is not pointer-
    /// or keyboard interactable.
    /// [command]: ../command/index.html

    /// An attempt was made to select an [element] that cannot be selected.
    /// [element]: ../common/struct.WebElement.html

    /// Navigation caused the user agent to hit a certificate warning, which is
    /// usually the result of an expired or invalid TLS certificate.

    /// The arguments passed to a [command] are either invalid or malformed.
    /// [command]: ../command/index.html

    /// An illegal attempt was made to set a cookie under a different domain
    /// than the current page.

    /// The coordinates provided to an interactions operation are invalid.

    /// A [command] could not be completed because the element is an invalid
    /// state, e.g. attempting to click an element that is no longer attached
    /// to the document.
    /// [command]: ../command/index.html

    /// Argument was an invalid selector.

    /// Occurs if the given session ID is not in the list of active sessions,
    /// meaning the session either does not exist or that it’s not active.

    /// An error occurred while executing JavaScript supplied by the user.

    /// The target for mouse interaction is not in the browser’s viewport and
    /// cannot be brought into that viewport.

    /// An attempt was made to operate on a modal dialogue when one was not
    /// open.

    /// No cookie matching the given path name was found amongst the associated
    /// cookies of the current browsing context’s active document.

    /// An [element] could not be located on the page using the given search
    /// parameters.
    /// [element]: ../common/struct.WebElement.html

    /// A [command] to switch to a frame could not be satisfied because the
    /// frame could not be found.
    /// [command]: ../command/index.html

    /// A [command] to switch to a window could not be satisfied because the
    /// window could not be found.
    /// [command]: ../command/index.html

    /// A script did not complete before its timeout expired.

    /// A new session could not be created.

    /// A [command] failed because the referenced [element] is no longer
    /// attached to the DOM.
    /// [command]: ../command/index.html
    /// [element]: ../common/struct.WebElement.html

    /// An operation did not complete before its timeout expired.

    /// A screen capture was made impossible.

    /// Setting the cookie’s value could not be done.

    /// A modal dialogue was open, blocking this operation.

    /// The requested command could not be executed because it does not exist.

    /// An unknown error occurred in the remote end whilst processing the
    /// [command].
    /// [command]: ../command/index.html

    /// The requested [command] matched a known endpoint, but did not match a
    /// method for that endpoint.
    /// [command]: ../command/index.html


    /// Indicates that a [command] that should have executed properly is not
    /// currently supported.

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",
            InvalidCookieDomain => "invalid cookie domain",
            InvalidCoordinates => "invalid coordinates",
            InvalidElementState => "invalid element state",
            InvalidSelector => "invalid selector",
            InvalidSessionId => "invalid session id",
            JavascriptError => "javascript error",
            MoveTargetOutOfBounds => "move target out of bounds",
            NoSuchAlert => "no such alert",
            NoSuchCookie => "no such cookie",
            NoSuchElement => "no such element",
            NoSuchFrame => "no such frame",
            NoSuchWindow => "no such window",
            ScriptTimeout => "script timeout",
            SessionNotCreated => "session not created",
            StaleElementReference => "stale element reference",
            Timeout => "timeout",
            UnableToCaptureScreen => "unable to capture screen",
            UnableToSetCookie => "unable to set cookie",
            UnexpectedAlertOpen => "unexpected alert open",
            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,
            InvalidArgument => BadRequest,
            InvalidCookieDomain => BadRequest,
            InvalidCoordinates => BadRequest,
            InvalidElementState => BadRequest,
            InvalidSelector => BadRequest,
            InvalidSessionId => NotFound,
            JavascriptError => InternalServerError,
            MoveTargetOutOfBounds => InternalServerError,
            NoSuchAlert => NotFound,
            NoSuchCookie => NotFound,
            NoSuchElement => NotFound,
            NoSuchFrame => NotFound,
            NoSuchWindow => NotFound,
            ScriptTimeout => RequestTimeout,
            SessionNotCreated => InternalServerError,
            StaleElementReference => BadRequest,
            Timeout => RequestTimeout,
            UnableToCaptureScreen => BadRequest,
            UnableToSetCookie => InternalServerError,
            UnexpectedAlertOpen => InternalServerError,
            UnknownCommand => NotFound,
            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>;

pub struct WebDriverError {
    pub error: ErrorStatus,
    pub message: Cow<'static, str>,
    pub stack: Cow<'static, str>,
    pub delete_session: bool,

impl WebDriverError {
    pub fn new<S>(error: ErrorStatus, message: S) -> WebDriverError
        where S: Into<Cow<'static, str>>
        WebDriverError {
            error: error,
            message: message.into(),
            stack: format!("{:?}", Backtrace::new()).into(),
            delete_session: false,

    pub fn new_with_stack<S>(error: ErrorStatus, message: S, stack: S) -> WebDriverError
        where S: Into<Cow<'static, str>>
        WebDriverError {
            error: error,
            message: message.into(),
            stack: stack.into(),
            delete_session: false,

    pub fn error_code(&self) -> &'static str {

    pub fn http_status(&self) -> StatusCode {

    pub fn to_json_string(&self) -> String {

impl ToJson for WebDriverError {
    fn to_json(&self) -> Json {
        let mut data = BTreeMap::new();
        data.insert("error".into(), self.error_code().to_json());
        data.insert("message".into(), self.message.to_json());
        data.insert("stacktrace".into(), self.stack.to_json());

        let mut wrapper = BTreeMap::new();
        wrapper.insert("value".into(), Json::Object(data));

impl Error for WebDriverError {
    fn description(&self) -> &str {

    fn cause(&self) -> Option<&Error> {

impl fmt::Display for WebDriverError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

impl From<ParserError> for WebDriverError {
    fn from(err: ParserError) -> WebDriverError {
        WebDriverError::new(ErrorStatus::UnknownError, err.description().to_string())

impl From<io::Error> for WebDriverError {
    fn from(err: io::Error) -> WebDriverError {
        WebDriverError::new(ErrorStatus::UnknownError, err.description().to_string())

impl From<DecoderError> for WebDriverError {
    fn from(err: DecoderError) -> WebDriverError {
        WebDriverError::new(ErrorStatus::UnknownError, err.description().to_string())

impl From<FromBase64Error> for WebDriverError {
    fn from(err: FromBase64Error) -> WebDriverError {
        WebDriverError::new(ErrorStatus::UnknownError, err.description().to_string())

impl From<Box<Error>> for WebDriverError {
    fn from(err: Box<Error>) -> WebDriverError {
        WebDriverError::new(ErrorStatus::UnknownError, err.description().to_string())