From 374e2c4d1f2d496143e40a2031cfc3189a228ada Mon Sep 17 00:00:00 2001 From: mediaminister Date: Sun, 29 Sep 2024 20:10:13 +0200 Subject: [PATCH] Improve error handling --- resources/lib/goplay/content.py | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/resources/lib/goplay/content.py b/resources/lib/goplay/content.py index b65b488..efaaff9 100644 --- a/resources/lib/goplay/content.py +++ b/resources/lib/goplay/content.py @@ -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): @@ -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 @@ -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 @@ -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 @@ -914,17 +906,15 @@ 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') @@ -932,7 +922,12 @@ def _get_error_message(response): message = response.json().get('errormsg') else: message = response.text - return message + + _LOGGER.error(message) + if response.status_code == 451: + raise GeoblockedException(message) + else: + 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 """