diff --git a/py/selenium/webdriver/remote/errorhandler.py b/py/selenium/webdriver/remote/errorhandler.py index fbfc7696d3986..9a100e20543f5 100644 --- a/py/selenium/webdriver/remote/errorhandler.py +++ b/py/selenium/webdriver/remote/errorhandler.py @@ -15,6 +15,7 @@ # specific language governing permissions and limitations # under the License. +import json from typing import Any from selenium.common.exceptions import ( @@ -161,21 +162,20 @@ def check_response(self, response: dict[str, Any]) -> None: if isinstance(status, int): value_json = response.get("value", None) if value_json and isinstance(value_json, str): - import json - try: value = json.loads(value_json) - if len(value) == 1: - value = value["value"] - status = value.get("error", None) - if not status: - status = value.get("status", ErrorCode.UNKNOWN_ERROR) - message = value.get("value") or value.get("message") - if not isinstance(message, str): - value = message - message = message.get("message") - else: - message = value.get("message", None) + if isinstance(value, dict): + if len(value) == 1: + value = value["value"] + status = value.get("error", None) + if not status: + status = value.get("status", ErrorCode.UNKNOWN_ERROR) + message = value.get("value") or value.get("message") + if not isinstance(message, str): + value = message + message = message.get("message") + else: + message = value.get("message", None) except ValueError: pass diff --git a/py/test/unit/selenium/webdriver/remote/error_handler_tests.py b/py/test/unit/selenium/webdriver/remote/error_handler_tests.py index 3d5afdad2bc7c..3b07a97394ceb 100644 --- a/py/test/unit/selenium/webdriver/remote/error_handler_tests.py +++ b/py/test/unit/selenium/webdriver/remote/error_handler_tests.py @@ -15,6 +15,8 @@ # specific language governing permissions and limitations # under the License. +import json + import pytest from selenium.common import exceptions @@ -249,20 +251,15 @@ def test_raises_exception_for_detached_shadow_root(handler, code): @pytest.mark.parametrize("key", ["stackTrace", "stacktrace"]) def test_relays_exception_stacktrace(handler, key): - import json - stacktrace = {"lineNumber": 100, "fileName": "egg", "methodName": "ham", "className": "Spam"} value = {key: [stacktrace], "message": "very bad", "error": ErrorCode.UNKNOWN_METHOD[0]} response = {"status": 400, "value": json.dumps({"value": value})} with pytest.raises(exceptions.UnknownMethodException) as e: handler.check_response(response) - assert "Spam.ham" in e.value.stacktrace[0] -def test_handle_errors_better(handler): - import json - +def test_handle_json_error_with_message(handler): response = { "status": 500, "value": json.dumps( @@ -281,5 +278,24 @@ def test_handle_errors_better(handler): } with pytest.raises(exceptions.WebDriverException) as e: handler.check_response(response) - assert "Could not start a new session." in e.value.msg + + +def test_handle_string_error(handler): + response = { + "status": 407, + "value": "Proxy Authentication Required", + } + with pytest.raises(exceptions.WebDriverException) as e: + handler.check_response(response) + assert e.value.msg == "Proxy Authentication Required" + + +def test_handle_numeric_error(handler): + response = { + "status": 999, + "value": "0", + } + with pytest.raises(exceptions.WebDriverException) as e: + handler.check_response(response) + assert e.value.msg == "0"