Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions py/selenium/webdriver/remote/errorhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# specific language governing permissions and limitations
# under the License.

import json
from typing import Any

from selenium.common.exceptions import (
Expand Down Expand Up @@ -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

Expand Down
30 changes: 23 additions & 7 deletions py/test/unit/selenium/webdriver/remote/error_handler_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# specific language governing permissions and limitations
# under the License.

import json

import pytest

from selenium.common import exceptions
Expand Down Expand Up @@ -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(
Expand All @@ -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"