mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-12 04:45:45 +00:00
Bug 1411045 - Provide response-to-error conversion. r=maja_zf
This adds a new error.from_response(wdclient.Response) function that lets a WebDriverException be constructed from a wdclient.Response object. If the Response is not an error, i.e. has a 200 HTTP status code, an UnknownErrorException is raised. MozReview-Commit-ID: JW89Ily2voC --HG-- extra : rebase_source : 5b126f3d62fc19a1ef0df7374167a7ee6828ab29
This commit is contained in:
parent
b49d1c1ffc
commit
38c8ff6a95
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user