Skip to content

Commit

Permalink
[CAT-171] & [CAT-168] Fix exception handling for hibernation and deco…
Browse files Browse the repository at this point in the history
…ding (#136)

* add exception and handle 500s before parsing content

* check for 503 not 502

* add after as a prop on the exception
  • Loading branch information
goatrocks authored Feb 7, 2022
1 parent f5dcbac commit af83ec7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
7 changes: 7 additions & 0 deletions indico/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

14 changes: 11 additions & 3 deletions indico/http/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand All @@ -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 = (
Expand Down

0 comments on commit af83ec7

Please sign in to comment.