Skip to content
This repository has been archived by the owner on Jan 7, 2024. It is now read-only.

Commit

Permalink
app: make direct HTTP json request function consistent with qrexec
Browse files Browse the repository at this point in the history
the behavior of the HTTP and qrexec JSON request methods must
be the same in terms of which exceptions are raised when
  • Loading branch information
redshiftzero authored and sssoleileraaa committed Feb 11, 2020
1 parent 42e49a1 commit 748adb6
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions sdclientapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def __init__(self) -> None:

class ServerConnectionError(Exception):
"""
Error raised if a request times out.
Error raised if we cannot connect to the server.
"""

def __init__(self) -> None:
super().__init__("The request timed out.")
super().__init__("Cannot connect to the server.")


def json_query(proxy_vm_name: str, data: str, timeout: Optional[int] = None) -> str:
Expand Down Expand Up @@ -166,6 +166,9 @@ def _send_http_json_request(
except (TooManyRedirects, ConnectionError):
raise ServerConnectionError

if result.status_code == http.HTTPStatus.FORBIDDEN:
raise AuthError("forbidden")

# Because when we download a file there is no JSON in the body
if path_query.endswith("/download"):
return result, result.status_code, dict(result.headers)
Expand Down Expand Up @@ -201,12 +204,13 @@ def _send_rpc_json_request(
raise RequestTimeoutError
elif "error" in data and result["status"] == http.HTTPStatus.BAD_GATEWAY:
raise ServerConnectionError
elif "error" in data and result["status"] == 403:
elif "error" in data and result["status"] == http.HTTPStatus.FORBIDDEN:
raise AuthError(data["error"])
elif "error" in data and result["status"] != 404:
elif "error" in data and result["status"] != http.HTTPStatus.NOT_FOUND:
# We exclude 404 since if we encounter a 404, it means that an
# item is missing. In that case we return to the caller to
# handle that with an appropriate message.
# handle that with an appropriate message. However, if the error
# is not a 404, then we raise.
raise BaseError(data["error"])

return data, result["status"], result["headers"]
Expand Down

0 comments on commit 748adb6

Please sign in to comment.