1<?php
2
3namespace Facebook\WebDriver\Exception;
4
5use Exception;
6
7/**
8 * @see https://w3c.github.io/webdriver/#errors
9 */
10class WebDriverException extends Exception
11{
12    private $results;
13
14    /**
15     * @param string $message
16     * @param mixed $results
17     */
18    public function __construct($message, $results = null)
19    {
20        parent::__construct($message);
21        $this->results = $results;
22    }
23
24    /**
25     * @return mixed
26     */
27    public function getResults()
28    {
29        return $this->results;
30    }
31
32    /**
33     * Throw WebDriverExceptions based on WebDriver status code.
34     *
35     * @param int|string $status_code
36     * @param string $message
37     * @param mixed $results
38     *
39     * @throws ElementClickInterceptedException
40     * @throws ElementNotInteractableException
41     * @throws ElementNotSelectableException
42     * @throws ElementNotVisibleException
43     * @throws ExpectedException
44     * @throws IMEEngineActivationFailedException
45     * @throws IMENotAvailableException
46     * @throws IndexOutOfBoundsException
47     * @throws InsecureCertificateException
48     * @throws InvalidArgumentException
49     * @throws InvalidCookieDomainException
50     * @throws InvalidCoordinatesException
51     * @throws InvalidElementStateException
52     * @throws InvalidSelectorException
53     * @throws InvalidSessionIdException
54     * @throws JavascriptErrorException
55     * @throws MoveTargetOutOfBoundsException
56     * @throws NoAlertOpenException
57     * @throws NoCollectionException
58     * @throws NoScriptResultException
59     * @throws NoStringException
60     * @throws NoStringLengthException
61     * @throws NoStringWrapperException
62     * @throws NoSuchAlertException
63     * @throws NoSuchCollectionException
64     * @throws NoSuchCookieException
65     * @throws NoSuchDocumentException
66     * @throws NoSuchDriverException
67     * @throws NoSuchElementException
68     * @throws NoSuchFrameException
69     * @throws NoSuchWindowException
70     * @throws NullPointerException
71     * @throws ScriptTimeoutException
72     * @throws SessionNotCreatedException
73     * @throws StaleElementReferenceException
74     * @throws TimeoutException
75     * @throws UnableToCaptureScreenException
76     * @throws UnableToSetCookieException
77     * @throws UnexpectedAlertOpenException
78     * @throws UnexpectedJavascriptException
79     * @throws UnknownCommandException
80     * @throws UnknownErrorException
81     * @throws UnknownMethodException
82     * @throws UnknownServerException
83     * @throws UnrecognizedExceptionException
84     * @throws UnsupportedOperationException
85     * @throws XPathLookupException
86     */
87    public static function throwException($status_code, $message, $results)
88    {
89        if (is_string($status_code)) {
90            // @see https://w3c.github.io/webdriver/#errors
91            switch ($status_code) {
92                case 'element click intercepted':
93                    throw new ElementClickInterceptedException($message, $results);
94                case 'element not interactable':
95                    throw new ElementNotInteractableException($message, $results);
96                case 'insecure certificate':
97                    throw new InsecureCertificateException($message, $results);
98                case 'invalid argument':
99                    throw new InvalidArgumentException($message, $results);
100                case 'invalid cookie domain':
101                    throw new InvalidCookieDomainException($message, $results);
102                case 'invalid element state':
103                    throw new InvalidElementStateException($message, $results);
104                case 'invalid selector':
105                    throw new InvalidSelectorException($message, $results);
106                case 'invalid session id':
107                    throw new InvalidSessionIdException($message, $results);
108                case 'javascript error':
109                    throw new JavascriptErrorException($message, $results);
110                case 'move target out of bounds':
111                    throw new MoveTargetOutOfBoundsException($message, $results);
112                case 'no such alert':
113                    throw new NoSuchAlertException($message, $results);
114                case 'no such cookie':
115                    throw new NoSuchCookieException($message, $results);
116                case 'no such element':
117                    throw new NoSuchElementException($message, $results);
118                case 'no such frame':
119                    throw new NoSuchFrameException($message, $results);
120                case 'no such window':
121                    throw new NoSuchWindowException($message, $results);
122                case 'script timeout':
123                    throw new ScriptTimeoutException($message, $results);
124                case 'session not created':
125                    throw new SessionNotCreatedException($message, $results);
126                case 'stale element reference':
127                    throw new StaleElementReferenceException($message, $results);
128                case 'timeout':
129                    throw new TimeoutException($message, $results);
130                case 'unable to set cookie':
131                    throw new UnableToSetCookieException($message, $results);
132                case 'unable to capture screen':
133                    throw new UnableToCaptureScreenException($message, $results);
134                case 'unexpected alert open':
135                    throw new UnexpectedAlertOpenException($message, $results);
136                case 'unknown command':
137                    throw new UnknownCommandException($message, $results);
138                case 'unknown error':
139                    throw new UnknownErrorException($message, $results);
140                case 'unknown method':
141                    throw new UnknownMethodException($message, $results);
142                case 'unsupported operation':
143                    throw new UnsupportedOperationException($message, $results);
144                default:
145                    throw new UnrecognizedExceptionException($message, $results);
146            }
147        }
148
149        switch ($status_code) {
150            case 1:
151                throw new IndexOutOfBoundsException($message, $results);
152            case 2:
153                throw new NoCollectionException($message, $results);
154            case 3:
155                throw new NoStringException($message, $results);
156            case 4:
157                throw new NoStringLengthException($message, $results);
158            case 5:
159                throw new NoStringWrapperException($message, $results);
160            case 6:
161                throw new NoSuchDriverException($message, $results);
162            case 7:
163                throw new NoSuchElementException($message, $results);
164            case 8:
165                throw new NoSuchFrameException($message, $results);
166            case 9:
167                throw new UnknownCommandException($message, $results);
168            case 10:
169                throw new StaleElementReferenceException($message, $results);
170            case 11:
171                throw new ElementNotVisibleException($message, $results);
172            case 12:
173                throw new InvalidElementStateException($message, $results);
174            case 13:
175                throw new UnknownServerException($message, $results);
176            case 14:
177                throw new ExpectedException($message, $results);
178            case 15:
179                throw new ElementNotSelectableException($message, $results);
180            case 16:
181                throw new NoSuchDocumentException($message, $results);
182            case 17:
183                throw new UnexpectedJavascriptException($message, $results);
184            case 18:
185                throw new NoScriptResultException($message, $results);
186            case 19:
187                throw new XPathLookupException($message, $results);
188            case 20:
189                throw new NoSuchCollectionException($message, $results);
190            case 21:
191                throw new TimeoutException($message, $results);
192            case 22:
193                throw new NullPointerException($message, $results);
194            case 23:
195                throw new NoSuchWindowException($message, $results);
196            case 24:
197                throw new InvalidCookieDomainException($message, $results);
198            case 25:
199                throw new UnableToSetCookieException($message, $results);
200            case 26:
201                throw new UnexpectedAlertOpenException($message, $results);
202            case 27:
203                throw new NoAlertOpenException($message, $results);
204            case 28:
205                throw new ScriptTimeoutException($message, $results);
206            case 29:
207                throw new InvalidCoordinatesException($message, $results);
208            case 30:
209                throw new IMENotAvailableException($message, $results);
210            case 31:
211                throw new IMEEngineActivationFailedException($message, $results);
212            case 32:
213                throw new InvalidSelectorException($message, $results);
214            case 33:
215                throw new SessionNotCreatedException($message, $results);
216            case 34:
217                throw new MoveTargetOutOfBoundsException($message, $results);
218            default:
219                throw new UnrecognizedExceptionException($message, $results);
220        }
221    }
222}
223