diff --git a/hypervector/errors.py b/hypervector/errors.py index 6a24b56..2b662bb 100644 --- a/hypervector/errors.py +++ b/hypervector/errors.py @@ -2,10 +2,17 @@ class APIKeyNotSetError(Exception): pass +class APIBaseError(Exception): + pass + + class HypervectorError(Exception): def __init__(self, response=None): self.response = response - self.status_code = response.status_code + if response: + self.status_code = response.status_code + else: + self.status_code = None diff --git a/hypervector/resources/abstract/api_resource.py b/hypervector/resources/abstract/api_resource.py index dbec8d6..1fede2d 100644 --- a/hypervector/resources/abstract/api_resource.py +++ b/hypervector/resources/abstract/api_resource.py @@ -1,6 +1,6 @@ import requests import hypervector -from hypervector.errors import APIKeyNotSetError, HypervectorError +from hypervector.errors import APIKeyNotSetError, HypervectorError, APIBaseError class APIResource: @@ -19,15 +19,23 @@ def get_headers(cls): @classmethod def get(cls, uuid): endpoint = f'{hypervector.API_BASE}/{cls.resource_name}/{uuid}' - response = requests.get(endpoint, headers=cls.get_headers()) + try: + response = requests.get(endpoint, headers=cls.get_headers()) + except requests.ConnectionError: + raise APIBaseError + if response.ok: return cls.from_get(response) else: - raise HypervectorError(response) + raise HypervectorError @classmethod def request(cls, endpoint, method=requests.get): - response = method(url=endpoint, headers=cls.get_headers()) + try: + response = method(url=endpoint, headers=cls.get_headers()) + except requests.ConnectionError: + raise APIBaseError + if response.ok: return response.json() else: