Skip to content

Truly independent web browser

5.4 KB
Raw Blame
/*
 * Copyright (c) 2022, Sam Atkins <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Error.h>
#include <AK/NeverDestroyed.h>
#include <AK/Vector.h>
#include <LibWebCommon/WebDriver/Error.h>

namespace Web::WebDriver {

struct ErrorCodeData {
    ErrorCode error_code;
    unsigned http_status;
    String json_error_code;
};

// https://w3c.github.io/webdriver/#dfn-error-code
static Vector<ErrorCodeData> const& error_code_data()
{
    static auto const& data = *new Vector<ErrorCodeData> {
        { ErrorCode::ElementClickIntercepted, 400, "element click intercepted"_string },
        { ErrorCode::ElementNotInteractable, 400, "element not interactable"_string },
        { ErrorCode::InsecureCertificate, 400, "insecure certificate"_string },
        { ErrorCode::InvalidArgument, 400, "invalid argument"_string },
        { ErrorCode::InvalidCookieDomain, 400, "invalid cookie domain"_string },
        { ErrorCode::InvalidElementState, 400, "invalid element state"_string },
        { ErrorCode::InvalidSelector, 400, "invalid selector"_string },
        { ErrorCode::InvalidSessionId, 404, "invalid session id"_string },
        { ErrorCode::JavascriptError, 500, "javascript error"_string },
        { ErrorCode::MoveTargetOutOfBounds, 500, "move target out of bounds"_string },
        { ErrorCode::NoSuchAlert, 404, "no such alert"_string },
        { ErrorCode::NoSuchCookie, 404, "no such cookie"_string },
        { ErrorCode::NoSuchElement, 404, "no such element"_string },
        { ErrorCode::NoSuchFrame, 404, "no such frame"_string },
        { ErrorCode::NoSuchWindow, 404, "no such window"_string },
        { ErrorCode::NoSuchShadowRoot, 404, "no such shadow root"_string },
        { ErrorCode::ScriptTimeoutError, 500, "script timeout"_string },
        { ErrorCode::SessionNotCreated, 500, "session not created"_string },
        { ErrorCode::StaleElementReference, 404, "stale element reference"_string },
        { ErrorCode::DetachedShadowRoot, 404, "detached shadow root"_string },
        { ErrorCode::Timeout, 500, "timeout"_string },
        { ErrorCode::UnableToSetCookie, 500, "unable to set cookie"_string },
        { ErrorCode::UnableToCaptureScreen, 500, "unable to capture screen"_string },
        { ErrorCode::UnexpectedAlertOpen, 500, "unexpected alert open"_string },
        { ErrorCode::UnknownCommand, 404, "unknown command"_string },
        { ErrorCode::UnknownError, 500, "unknown error"_string },
        { ErrorCode::UnknownMethod, 405, "unknown method"_string },
        { ErrorCode::UnsupportedOperation, 500, "unsupported operation"_string },
        // https://w3c.github.io/webdriver-bidi/#errors
        // NB: These errors are only sent over WebDriver BiDi, which has no HTTP status codes.
        { ErrorCode::InvalidWebExtension, 500, "invalid web extension"_string },
        { ErrorCode::NoSuchClientWindow, 500, "no such client window"_string },
        { ErrorCode::NoSuchHandle, 500, "no such handle"_string },
        { ErrorCode::NoSuchHistoryEntry, 500, "no such history entry"_string },
        { ErrorCode::NoSuchNetworkCollector, 500, "no such network collector"_string },
        { ErrorCode::NoSuchIntercept, 500, "no such intercept"_string },
        { ErrorCode::NoSuchNetworkData, 500, "no such network data"_string },
        { ErrorCode::NoSuchNode, 500, "no such node"_string },
        { ErrorCode::NoSuchRequest, 500, "no such request"_string },
        { ErrorCode::NoSuchScreencast, 500, "no such screencast"_string },
        { ErrorCode::NoSuchScript, 500, "no such script"_string },
        { ErrorCode::NoSuchStoragePartition, 500, "no such storage partition"_string },
        { ErrorCode::NoSuchUserContext, 500, "no such user context"_string },
        { ErrorCode::NoSuchWebExtension, 500, "no such web extension"_string },
        { ErrorCode::UnableToCloseBrowser, 500, "unable to close browser"_string },
        { ErrorCode::UnderspecifiedStoragePartition, 500, "underspecified storage partition"_string },
        { ErrorCode::UnableToSetFileInput, 500, "unable to set file input"_string },
        { ErrorCode::UnavailableNetworkData, 500, "unavailable network data"_string },
        { ErrorCode::OutOfMemory, 500, "out of memory"_string },
    };
    return data;
}

Error Error::from_code(ErrorCode code, String message, Optional<JsonValue> data)
{
    auto const& error_code_data = WebDriver::error_code_data()[to_underlying(code)];

    return {
        error_code_data.http_status,
        error_code_data.json_error_code,
        move(message),
        move(data)
    };
}

Error Error::from_code(ErrorCode code, StringView message, Optional<JsonValue> data)
{
    return from_code(code, String::from_utf8_without_validation(message.bytes()), move(data));
}

Error::Error(AK::Error const& error)
{
    VERIFY(error.code() == ENOMEM);
    *this = from_code(ErrorCode::OutOfMemory, String {}, {});
}

Error::Error(unsigned http_status_, String error_, String message_, Optional<JsonValue> data_)
    : http_status(http_status_)
    , error(move(error_))
    , message(move(message_))
    , data(move(data_))
{
}

Error::Error(Error const& error)
    : http_status(error.http_status)
    , error(error.error)
    , message(error.message)
    , data(error.data)
{
}

Error& Error::operator=(Error const& other)
{
    http_status = other.http_status;
    error = other.error;
    message = other.message;
    data = other.data;
    return *this;
}

}