From 166332a66928408d8343b2aad9b45cba01fef557 Mon Sep 17 00:00:00 2001 From: Daan Koning Date: Mon, 23 Jan 2023 13:55:12 +0100 Subject: [PATCH] Improve error handling --- logic.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/logic.py b/logic.py index 4b1e4d3..59ef550 100644 --- a/logic.py +++ b/logic.py @@ -13,7 +13,13 @@ PROTOCOL = "https://" class APIException(RuntimeError): - pass + def __int__(self, message, response: requests.Response): + self.api_message = response.json()['message'] + + super().__init__(message) + + def __str__(self): + return f"('{self.args[0]}', '{self.args[1].json()['message']}')" class AuthenticationException(APIException): @@ -26,11 +32,11 @@ class RateLimitException(APIException): def handle_faulty_response(response: requests.Response): if response.status_code == 401: - raise AuthenticationException("Failed to authenticate with the API. is the API key valid?") + raise AuthenticationException("Failed to authenticate with the API. is the API key valid?", response) elif response.status_code == 429: - raise RateLimitException("Encountered API rate limit.") + raise RateLimitException("Encountered API rate limit.", response) else: - raise APIException("Unknown issue arose while trying to access the API.") + raise APIException("Unknown issue arose while trying to access the API.", response) def get_sports(key: str) -> set[str]: