@@ -122,49 +122,46 @@ protected function curl($requestMethod, $command, $parameters = null, $extraOpti
122122
123123 list ($ rawResult , $ info ) = ServiceFactory::getInstance ()->getService ('service.curl ' )->execute ($ requestMethod , $ url , $ parameters , $ extraOptions );
124124
125- // According to https://w3c.github.io/webdriver/webdriver-spec.html all 4xx responses are to be considered an error and return plaintext,
126- // while 5xx responses are json encoded
127- if (substr ($ info ['http_code ' ], 0 , 1 ) === '4 ' ) {
128- throw WebDriverException::factory (WebDriverException::CURL_EXEC , 'Webdriver http error: ' . $ info ['http_code ' ] . ', payload : ' . substr ($ rawResult , 0 , 1000 ));
129- }
130-
131- $ result = [];
132- $ value = null ;
133- if (!empty ($ rawResult )) {
134- $ result = json_decode ($ rawResult , true );
125+ $ httpCode = $ info ['http_code ' ];
135126
136- if ($ result === null && json_last_error () != JSON_ERROR_NONE ) {
137- throw WebDriverException::factory (WebDriverException::CURL_EXEC ,
138- 'Payload received from webdriver is not valid json: ' . substr ($ rawResult , 0 , 1000 ));
139- }
127+ // According to https://w3c.github.io/webdriver/webdriver-spec.html all 4xx responses are to be considered
128+ // an error and return plaintext, while 5xx responses are json encoded
129+ if ($ httpCode >= 400 && $ httpCode <= 499 ) {
130+ throw WebDriverException::factory (
131+ WebDriverException::CURL_EXEC ,
132+ 'Webdriver http error: ' . $ httpCode . ', payload : ' . substr ($ rawResult , 0 , 1000 )
133+ );
134+ }
140135
141- if (!is_array ($ result ) || !array_key_exists ('status ' , $ result )) {
142- throw WebDriverException::factory (WebDriverException::CURL_EXEC ,
143- 'Payload received from webdriver is valid but unexpected json: ' . substr ($ rawResult , 0 , 1000 ));
144- }
136+ $ result = json_decode ($ rawResult , true );
145137
146- if (array_key_exists ('value ' , $ result )) {
147- $ value = $ result ['value ' ];
148- }
138+ if ($ result === null && json_last_error () != JSON_ERROR_NONE ) {
139+ throw WebDriverException::factory (
140+ WebDriverException::CURL_EXEC ,
141+ 'Payload received from webdriver is not valid json: ' . substr ($ rawResult , 0 , 1000 )
142+ );
143+ }
149144
150- $ message = null ;
145+ if (!is_array ($ result ) || !array_key_exists ('status ' , $ result )) {
146+ throw WebDriverException::factory (
147+ WebDriverException::CURL_EXEC ,
148+ 'Payload received from webdriver is valid but unexpected json: ' . substr ($ rawResult , 0 , 1000 )
149+ );
150+ }
151151
152- if (is_array ($ value ) && array_key_exists ('message ' , $ value )) {
153- $ message = $ value ['message ' ];
154- }
152+ $ value = array_key_exists ('value ' , $ result ) ? $ result ['value ' ] : null ;
153+ $ message = (is_array ($ value ) && array_key_exists ('message ' , $ value )) ? $ value ['message ' ] : null ;
155154
156- // if not success, throw exception
157- if ((int ) $ result ['status ' ] !== 0 ) {
158- throw WebDriverException::factory ($ result ['status ' ], $ message );
159- }
155+ // if not success, throw exception
156+ if ((int ) $ result ['status ' ] !== 0 ) {
157+ throw WebDriverException::factory ($ result ['status ' ], $ message );
160158 }
161159
162160 $ sessionId = isset ($ result ['sessionId ' ])
163161 ? $ result ['sessionId ' ]
164- : (
165- isset ($ value ['webdriver.remote.sessionid ' ])
166- ? $ value ['webdriver.remote.sessionid ' ]
167- : null
162+ : (isset ($ value ['webdriver.remote.sessionid ' ])
163+ ? $ value ['webdriver.remote.sessionid ' ]
164+ : null
168165 );
169166
170167 return array (
0 commit comments