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

Commit

Permalink
app: add exception class for ConnectionError, TooManyRedirects
Browse files Browse the repository at this point in the history
  • Loading branch information
redshiftzero committed Feb 7, 2020
1 parent 2287226 commit c49ccdf
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion sdclientapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import os
import requests
from datetime import datetime
from requests.exceptions import ConnectTimeout, ReadTimeout
from requests.exceptions import (
ConnectTimeout,
ReadTimeout,
ConnectionError,
TooManyRedirects
)
from subprocess import PIPE, Popen, TimeoutExpired
from typing import List, Tuple, Dict, Optional, Any
from urllib.parse import urljoin
Expand Down Expand Up @@ -33,6 +38,15 @@ def __init__(self) -> None:
super().__init__("The request timed out.")


class ServerConnectionError(Exception):
"""
Error raised if a request times out.
"""

def __init__(self) -> None:
super().__init__("The request timed out.")


def json_query(proxy_vm_name: str, data: str, timeout: Optional[int] = None) -> str:
"""
Takes a JSON based query and passes it to the network proxy.
Expand Down Expand Up @@ -149,6 +163,8 @@ def _send_http_json_request(
result = requests.request(method, url, **kwargs)
except (ConnectTimeout, ReadTimeout):
raise RequestTimeoutError
except (TooManyRedirects, ConnectionError):
raise ServerConnectionError

# Because when we download a file there is no JSON in the body
if path_query.endswith("/download"):
Expand Down Expand Up @@ -183,6 +199,8 @@ def _send_rpc_json_request(

if "error" in data and result["status"] == http.HTTPStatus.GATEWAY_TIMEOUT:
raise RequestTimeoutError
elif "error" in data and result["status"] == http.HTTPStatus.BAD_GATEWAY:
raise ServerConnectionError
elif "error" in data and result["status"] == 403:
raise AuthError(data["error"])
elif "error" in data and result["status"] != 404:
Expand Down

0 comments on commit c49ccdf

Please sign in to comment.