Serenity Operating System
1/*
2 * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <AK/Error.h>
8#include <AK/Vector.h>
9#include <LibWeb/WebDriver/Error.h>
10
11namespace Web::WebDriver {
12
13struct ErrorCodeData {
14 ErrorCode error_code;
15 unsigned http_status;
16 DeprecatedString json_error_code;
17};
18
19// https://w3c.github.io/webdriver/#dfn-error-code
20static Vector<ErrorCodeData> const s_error_code_data = {
21 { ErrorCode::ElementClickIntercepted, 400, "element click intercepted" },
22 { ErrorCode::ElementNotInteractable, 400, "element not interactable" },
23 { ErrorCode::InsecureCertificate, 400, "insecure certificate" },
24 { ErrorCode::InvalidArgument, 400, "invalid argument" },
25 { ErrorCode::InvalidCookieDomain, 400, "invalid cookie domain" },
26 { ErrorCode::InvalidElementState, 400, "invalid element state" },
27 { ErrorCode::InvalidSelector, 400, "invalid selector" },
28 { ErrorCode::InvalidSessionId, 404, "invalid session id" },
29 { ErrorCode::JavascriptError, 500, "javascript error" },
30 { ErrorCode::MoveTargetOutOfBounds, 500, "move target out of bounds" },
31 { ErrorCode::NoSuchAlert, 404, "no such alert" },
32 { ErrorCode::NoSuchCookie, 404, "no such cookie" },
33 { ErrorCode::NoSuchElement, 404, "no such element" },
34 { ErrorCode::NoSuchFrame, 404, "no such frame" },
35 { ErrorCode::NoSuchWindow, 404, "no such window" },
36 { ErrorCode::NoSuchShadowRoot, 404, "no such shadow root" },
37 { ErrorCode::ScriptTimeoutError, 500, "script timeout" },
38 { ErrorCode::SessionNotCreated, 500, "session not created" },
39 { ErrorCode::StaleElementReference, 404, "stale element reference" },
40 { ErrorCode::DetachedShadowRoot, 404, "detached shadow root" },
41 { ErrorCode::Timeout, 500, "timeout" },
42 { ErrorCode::UnableToSetCookie, 500, "unable to set cookie" },
43 { ErrorCode::UnableToCaptureScreen, 500, "unable to capture screen" },
44 { ErrorCode::UnexpectedAlertOpen, 500, "unexpected alert open" },
45 { ErrorCode::UnknownCommand, 404, "unknown command" },
46 { ErrorCode::UnknownError, 500, "unknown error" },
47 { ErrorCode::UnknownMethod, 405, "unknown method" },
48 { ErrorCode::UnsupportedOperation, 500, "unsupported operation" },
49 { ErrorCode::OutOfMemory, 500, "out of memory" },
50};
51
52Error Error::from_code(ErrorCode code, DeprecatedString message, Optional<JsonValue> data)
53{
54 auto const& error_code_data = s_error_code_data[to_underlying(code)];
55
56 return {
57 error_code_data.http_status,
58 error_code_data.json_error_code,
59 move(message),
60 move(data)
61 };
62}
63
64Error::Error(AK::Error const& error)
65{
66 VERIFY(error.code() == ENOMEM);
67 *this = from_code(ErrorCode::OutOfMemory, {}, {});
68}
69
70Error::Error(unsigned http_status_, DeprecatedString error_, DeprecatedString message_, Optional<JsonValue> data_)
71 : http_status(http_status_)
72 , error(move(error_))
73 , message(move(message_))
74 , data(move(data_))
75{
76}
77
78}