1 | /* |
2 | * Copyright (C) 2017 Igalia S.L. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include <wtf/Forward.h> |
29 | #include <wtf/JSONValues.h> |
30 | #include <wtf/text/WTFString.h> |
31 | |
32 | namespace WebDriver { |
33 | |
34 | class CommandResult { |
35 | public: |
36 | // ยง6.6 Handling Errors. |
37 | // https://www.w3.org/TR/webdriver/#handling-errors |
38 | enum class ErrorCode { |
39 | ElementClickIntercepted, |
40 | ElementNotSelectable, |
41 | ElementNotInteractable, |
42 | InvalidArgument, |
43 | InvalidElementState, |
44 | InvalidSelector, |
45 | InvalidSessionID, |
46 | JavascriptError, |
47 | MoveTargetOutOfBounds, |
48 | NoSuchAlert, |
49 | NoSuchCookie, |
50 | NoSuchElement, |
51 | NoSuchFrame, |
52 | NoSuchWindow, |
53 | ScriptTimeout, |
54 | SessionNotCreated, |
55 | StaleElementReference, |
56 | Timeout, |
57 | UnableToCaptureScreen, |
58 | UnexpectedAlertOpen, |
59 | UnknownCommand, |
60 | UnknownError, |
61 | UnsupportedOperation, |
62 | }; |
63 | |
64 | static CommandResult success(RefPtr<JSON::Value>&& result = nullptr) |
65 | { |
66 | return CommandResult(WTFMove(result)); |
67 | } |
68 | |
69 | static CommandResult fail(RefPtr<JSON::Value>&& result = nullptr) |
70 | { |
71 | return CommandResult(WTFMove(result), CommandResult::ErrorCode::UnknownError); |
72 | } |
73 | |
74 | static CommandResult fail(ErrorCode errorCode, Optional<String> errorMessage = WTF::nullopt) |
75 | { |
76 | return CommandResult(errorCode, errorMessage); |
77 | } |
78 | |
79 | unsigned httpStatusCode() const; |
80 | const RefPtr<JSON::Value>& result() const { return m_result; }; |
81 | void setAdditionalErrorData(RefPtr<JSON::Object>&& errorData) { m_errorAdditionalData = WTFMove(errorData); } |
82 | bool isError() const { return !!m_errorCode; } |
83 | ErrorCode errorCode() const { ASSERT(isError()); return m_errorCode.value(); } |
84 | String errorString() const; |
85 | Optional<String> errorMessage() const { ASSERT(isError()); return m_errorMessage; } |
86 | const RefPtr<JSON::Object>& additionalErrorData() const { return m_errorAdditionalData; } |
87 | |
88 | private: |
89 | explicit CommandResult(RefPtr<JSON::Value>&&, Optional<ErrorCode> = WTF::nullopt); |
90 | explicit CommandResult(ErrorCode, Optional<String> = WTF::nullopt); |
91 | |
92 | RefPtr<JSON::Value> m_result; |
93 | Optional<ErrorCode> m_errorCode; |
94 | Optional<String> m_errorMessage; |
95 | RefPtr<JSON::Object> m_errorAdditionalData; |
96 | }; |
97 | |
98 | } // namespace WebDriver |
99 | |