From af83ec79eb5b445942ea900fec4b8fc580d79979 Mon Sep 17 00:00:00 2001 From: m c <458905+goatrocks@users.noreply.github.com> Date: Mon, 7 Feb 2022 11:25:52 -0500 Subject: [PATCH] [CAT-171] & [CAT-168] Fix exception handling for hibernation and decoding (#136) * add exception and handle 500s before parsing content * check for 503 not 502 * add after as a prop on the exception --- indico/errors.py | 7 +++++++ indico/http/client.py | 14 +++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/indico/errors.py b/indico/errors.py index 9542a109..515ee6f9 100644 --- a/indico/errors.py +++ b/indico/errors.py @@ -38,3 +38,10 @@ def __init__(self, cls): class IndicoAuthenticationFailed(IndicoError): def __init__(self): super().__init__("Failed to authenticate") + + +class IndicoHibernationError(IndicoError): + def __init__(self, after): + self.after = after + super().__init__(f"Platform is currently hibernating. Wait {after} seconds and retry this request.") + diff --git a/indico/http/client.py b/indico/http/client.py index 96d55a88..f4bd9d54 100644 --- a/indico/http/client.py +++ b/indico/http/client.py @@ -9,7 +9,7 @@ import requests from indico.client.request import HTTPRequest from indico.config import IndicoConfig -from indico.errors import IndicoAuthenticationFailed, IndicoRequestError +from indico.errors import IndicoAuthenticationFailed, IndicoRequestError, IndicoHibernationError from indico.http.serialization import deserialize from requests import Response @@ -155,8 +155,6 @@ def _make_request( if len(url_parts) > 1 and (url_parts[-1] == "json" or url_parts[-2] == "json"): json = True - content = deserialize(response, force_json=json) - # If auth expired refresh if response.status_code == 401 and not _refreshed: self.get_short_lived_access_token() @@ -166,6 +164,16 @@ def _make_request( elif response.status_code == 401 and _refreshed: raise IndicoAuthenticationFailed() + if response.status_code == 503 and 'Retry-After' in response.headers: + raise IndicoHibernationError(after=response.headers.get('Retry-After')) + + if response.status_code >= 500: + raise IndicoRequestError( + code=response.status_code + ) + + content = deserialize(response, force_json=json) + if response.status_code >= 400: if isinstance(content, dict): error = (