diff --git a/testing/web-platform/tests/tools/webdriver/webdriver/client.py b/testing/web-platform/tests/tools/webdriver/webdriver/client.py index eb0734ba6bd2..efb665ff9f53 100644 --- a/testing/web-platform/tests/tools/webdriver/webdriver/client.py +++ b/testing/web-platform/tests/tools/webdriver/webdriver/client.py @@ -422,22 +422,23 @@ class Session(object): :param body: Optional body of the HTTP request. :return: `None` if the HTTP response body was empty, otherwise - the result of parsing the body as JSON. + the `value` field returned after parsing the response + body as JSON. :raises error.WebDriverException: If the remote end returns an error. """ response = self.transport.send(method, url, body) + if response.status != 200: + raise error.from_response(response) + if "value" in response.body: value = response.body["value"] else: - raise error.UnknownErrorException("No 'value' key in response body:\n%s" % - json.dumps(response.body)) - - if response.status != 200: - cls = error.get(value.get("error")) - raise cls(value.get("message")) + raise error.UnknownErrorException( + "Expected 'value' key in response body:\n" + "%s" % json.dumps(response.body)) return value diff --git a/testing/web-platform/tests/tools/webdriver/webdriver/error.py b/testing/web-platform/tests/tools/webdriver/webdriver/error.py index 9d27a78d51ab..01ab31b8af1c 100644 --- a/testing/web-platform/tests/tools/webdriver/webdriver/error.py +++ b/testing/web-platform/tests/tools/webdriver/webdriver/error.py @@ -1,4 +1,5 @@ import collections +import json class WebDriverException(Exception): @@ -145,6 +146,34 @@ class UnsupportedOperationException(WebDriverException): status_code = "unsupported operation" +def from_response(response): + """ + Unmarshals an error from a ``Response``'s `body`, failing + if not all three required `error`, `message`, and `stacktrace` + fields are given. Defaults to ``WebDriverException`` if `error` + is unknown. + """ + if response.status == 200: + raise UnknownErrorException( + "Response is not an error:\n" + "%s" % json.dumps(response.body)) + + if "value" in response.body: + value = response.body["value"] + else: + raise UnknownErrorException( + "Expected 'value' key in response body:\n" + "%s" % json.dumps(response.body)) + + # all fields must exist, but stacktrace can be an empty string + code = value["error"] + message = value["message"] + stack = value["stacktrace"] or None + + cls = get(code) + return cls(message, stacktrace=stack) + + def get(error_code): """ Gets exception from `error_code`, falling back to