Skip to content

Commit

Permalink
Improve error handling (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
mediaminister authored Sep 29, 2024
1 parent 49fd88e commit 2c7e387
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions resources/lib/goplay/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,6 @@ def get_stream(self, uuid, content_type):
stream_type=STREAM_DASH,
license_key=self.create_license_key(self.LICENSE_URL, key_headers=key_headers, device_path=device_path, manifest_url=ad_data['stream_manifest']),
)
if data.get('message'):
raise GeoblockedException(data)
raise UnavailableException

def get_program_tree(self):
Expand Down Expand Up @@ -851,9 +849,7 @@ def _get_url(self, url, params=None, headers=None, authentication=None):
response = self._session.get(url, params=params, headers=headers, proxies=PROXIES)
response.raise_for_status()
except requests.exceptions.HTTPError:
message = self._get_error_message(response)
_LOGGER.error(message)
raise ApiException(message)
self._handle_error_message(response)

return response.text

Expand All @@ -872,9 +868,7 @@ def _post_url(self, url, params=None, headers=None, data=None, authentication=No
response = self._session.post(url, params=params, headers=headers, data=data, proxies=PROXIES)
response.raise_for_status()
except requests.exceptions.HTTPError:
message = self._get_error_message(response)
_LOGGER.error(message)
raise ApiException(message)
self._handle_error_message(response)

return response.content

Expand All @@ -893,9 +887,7 @@ def _put_url(self, url, params=None, headers=None, data=None, authentication=Non
response = self._session.put(url, params=params, headers=headers, json=data, proxies=PROXIES)
response.raise_for_status()
except requests.exceptions.HTTPError:
message = self._get_error_message(response)
_LOGGER.error(message)
raise ApiException(message)
self._handle_error_message(response)

return response.text

Expand All @@ -914,25 +906,27 @@ def _delete_url(self, url, params=None, headers=None, authentication=None):
response = self._session.delete(url, params=params, headers=headers, proxies=PROXIES)
response.raise_for_status()
except requests.exceptions.HTTPError:
message = self._get_error_message(response)
_LOGGER.error(message)
raise ApiException(message)
self._handle_error_message(response)

return response.text


@staticmethod
def _get_error_message(response):
def _handle_error_message(response):
""" Returns the error message of an Api request.
:type response: requests.Response Object
:rtype str
"""
if response.json().get('message'):
message = response.json().get('message')
elif response.json().get('errormsg'):
message = response.json().get('errormsg')
else:
message = response.text
return message

_LOGGER.error(message)
if response.status_code == 451:
raise GeoblockedException(message)
raise ApiException(message)

def _handle_cache(self, key, cache_mode, update, ttl=30 * 24 * 60 * 60):
""" Fetch something from the cache, and update if needed """
Expand Down

0 comments on commit 2c7e387

Please sign in to comment.