From 0246abec8001cf22312f6453f6080ddf24a11f16 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Tue, 8 Nov 2022 10:10:27 -0500 Subject: LibWeb+WebDriver: Move WebDriverError to Web::WebDriver::Error This is to prepare for WebContent becoming the WebDriver client. --- Userland/Libraries/LibWeb/CMakeLists.txt | 1 + Userland/Libraries/LibWeb/WebDriver/Error.cpp | 61 +++++++++++++++++++++++++ Userland/Libraries/LibWeb/WebDriver/Error.h | 65 +++++++++++++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 Userland/Libraries/LibWeb/WebDriver/Error.cpp create mode 100644 Userland/Libraries/LibWeb/WebDriver/Error.h (limited to 'Userland/Libraries') diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 55a91a57dd..da87a6c0db 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -438,6 +438,7 @@ set(SOURCES WebAssembly/WebAssemblyTableConstructor.cpp WebAssembly/WebAssemblyTableObject.cpp WebAssembly/WebAssemblyTablePrototype.cpp + WebDriver/Error.cpp WebDriver/ExecuteScript.cpp WebGL/WebGLContextAttributes.cpp WebGL/WebGLContextEvent.cpp diff --git a/Userland/Libraries/LibWeb/WebDriver/Error.cpp b/Userland/Libraries/LibWeb/WebDriver/Error.cpp new file mode 100644 index 0000000000..2fdc3e6487 --- /dev/null +++ b/Userland/Libraries/LibWeb/WebDriver/Error.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2022, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +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 const s_error_code_data = { + { ErrorCode::ElementClickIntercepted, 400, "element click intercepted" }, + { ErrorCode::ElementNotInteractable, 400, "element not interactable" }, + { ErrorCode::InsecureCertificate, 400, "insecure certificate" }, + { ErrorCode::InvalidArgument, 400, "invalid argument" }, + { ErrorCode::InvalidCookieDomain, 400, "invalid cookie domain" }, + { ErrorCode::InvalidElementState, 400, "invalid element state" }, + { ErrorCode::InvalidSelector, 400, "invalid selector" }, + { ErrorCode::InvalidSessionId, 404, "invalid session id" }, + { ErrorCode::JavascriptError, 500, "javascript error" }, + { ErrorCode::MoveTargetOutOfBounds, 500, "move target out of bounds" }, + { ErrorCode::NoSuchAlert, 404, "no such alert" }, + { ErrorCode::NoSuchCookie, 404, "no such cookie" }, + { ErrorCode::NoSuchElement, 404, "no such element" }, + { ErrorCode::NoSuchFrame, 404, "no such frame" }, + { ErrorCode::NoSuchWindow, 404, "no such window" }, + { ErrorCode::NoSuchShadowRoot, 404, "no such shadow root" }, + { ErrorCode::ScriptTimeoutError, 500, "script timeout" }, + { ErrorCode::SessionNotCreated, 500, "session not created" }, + { ErrorCode::StaleElementReference, 404, "stale element reference" }, + { ErrorCode::DetachedShadowRoot, 404, "detached shadow root" }, + { ErrorCode::Timeout, 500, "timeout" }, + { ErrorCode::UnableToSetCookie, 500, "unable to set cookie" }, + { ErrorCode::UnableToCaptureScreen, 500, "unable to capture screen" }, + { ErrorCode::UnexpectedAlertOpen, 500, "unexpected alert open" }, + { ErrorCode::UnknownCommand, 404, "unknown command" }, + { ErrorCode::UnknownError, 500, "unknown error" }, + { ErrorCode::UnknownMethod, 405, "unknown method" }, + { ErrorCode::UnsupportedOperation, 500, "unsupported operation" }, +}; + +Error Error::from_code(ErrorCode code, String message, Optional data) +{ + auto const& error_code_data = s_error_code_data[to_underlying(code)]; + return { + .http_status = error_code_data.http_status, + .error = error_code_data.json_error_code, + .message = move(message), + .data = move(data) + }; +} + +} diff --git a/Userland/Libraries/LibWeb/WebDriver/Error.h b/Userland/Libraries/LibWeb/WebDriver/Error.h new file mode 100644 index 0000000000..4bb2c3aa1c --- /dev/null +++ b/Userland/Libraries/LibWeb/WebDriver/Error.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2022, Florent Castelli + * Copyright (c) 2022, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +namespace Web::WebDriver { + +// https://w3c.github.io/webdriver/#dfn-error-code +enum class ErrorCode { + ElementClickIntercepted, + ElementNotInteractable, + InsecureCertificate, + InvalidArgument, + InvalidCookieDomain, + InvalidElementState, + InvalidSelector, + InvalidSessionId, + JavascriptError, + MoveTargetOutOfBounds, + NoSuchAlert, + NoSuchCookie, + NoSuchElement, + NoSuchFrame, + NoSuchWindow, + NoSuchShadowRoot, + ScriptTimeoutError, + SessionNotCreated, + StaleElementReference, + DetachedShadowRoot, + Timeout, + UnableToSetCookie, + UnableToCaptureScreen, + UnexpectedAlertOpen, + UnknownCommand, + UnknownError, + UnknownMethod, + UnsupportedOperation, +}; + +// https://w3c.github.io/webdriver/#errors +struct Error { + unsigned http_status; + String error; + String message; + Optional data; + + static Error from_code(ErrorCode, String message, Optional data = {}); +}; + +} + +template<> +struct AK::Formatter : Formatter { + ErrorOr format(FormatBuilder& builder, Web::WebDriver::Error const& error) + { + return Formatter::format(builder, String::formatted("Error {}, {}: {}", error.http_status, error.error, error.message)); + } +}; -- cgit v1.2.3