Skip to content

Commit 3323450

Browse files
authored
[py] Fix error handler for non-json response bodies (#15887)
Fix error handler `check_response` for non-json responses from webdriver so it doesn't raise a cryptic error.
1 parent 4a6303e commit 3323450

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

py/selenium/webdriver/remote/errorhandler.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18+
import json
1819
from typing import Any
1920

2021
from selenium.common.exceptions import (
@@ -161,21 +162,20 @@ def check_response(self, response: dict[str, Any]) -> None:
161162
if isinstance(status, int):
162163
value_json = response.get("value", None)
163164
if value_json and isinstance(value_json, str):
164-
import json
165-
166165
try:
167166
value = json.loads(value_json)
168-
if len(value) == 1:
169-
value = value["value"]
170-
status = value.get("error", None)
171-
if not status:
172-
status = value.get("status", ErrorCode.UNKNOWN_ERROR)
173-
message = value.get("value") or value.get("message")
174-
if not isinstance(message, str):
175-
value = message
176-
message = message.get("message")
177-
else:
178-
message = value.get("message", None)
167+
if isinstance(value, dict):
168+
if len(value) == 1:
169+
value = value["value"]
170+
status = value.get("error", None)
171+
if not status:
172+
status = value.get("status", ErrorCode.UNKNOWN_ERROR)
173+
message = value.get("value") or value.get("message")
174+
if not isinstance(message, str):
175+
value = message
176+
message = message.get("message")
177+
else:
178+
message = value.get("message", None)
179179
except ValueError:
180180
pass
181181

py/test/unit/selenium/webdriver/remote/error_handler_tests.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18+
import json
19+
1820
import pytest
1921

2022
from selenium.common import exceptions
@@ -249,20 +251,15 @@ def test_raises_exception_for_detached_shadow_root(handler, code):
249251

250252
@pytest.mark.parametrize("key", ["stackTrace", "stacktrace"])
251253
def test_relays_exception_stacktrace(handler, key):
252-
import json
253-
254254
stacktrace = {"lineNumber": 100, "fileName": "egg", "methodName": "ham", "className": "Spam"}
255255
value = {key: [stacktrace], "message": "very bad", "error": ErrorCode.UNKNOWN_METHOD[0]}
256256
response = {"status": 400, "value": json.dumps({"value": value})}
257257
with pytest.raises(exceptions.UnknownMethodException) as e:
258258
handler.check_response(response)
259-
260259
assert "Spam.ham" in e.value.stacktrace[0]
261260

262261

263-
def test_handle_errors_better(handler):
264-
import json
265-
262+
def test_handle_json_error_with_message(handler):
266263
response = {
267264
"status": 500,
268265
"value": json.dumps(
@@ -281,5 +278,24 @@ def test_handle_errors_better(handler):
281278
}
282279
with pytest.raises(exceptions.WebDriverException) as e:
283280
handler.check_response(response)
284-
285281
assert "Could not start a new session." in e.value.msg
282+
283+
284+
def test_handle_string_error(handler):
285+
response = {
286+
"status": 407,
287+
"value": "Proxy Authentication Required",
288+
}
289+
with pytest.raises(exceptions.WebDriverException) as e:
290+
handler.check_response(response)
291+
assert e.value.msg == "Proxy Authentication Required"
292+
293+
294+
def test_handle_numeric_error(handler):
295+
response = {
296+
"status": 999,
297+
"value": "0",
298+
}
299+
with pytest.raises(exceptions.WebDriverException) as e:
300+
handler.check_response(response)
301+
assert e.value.msg == "0"

0 commit comments

Comments
 (0)