diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ad3aa5..7c826c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. +## [1.1.3] - 2022-09-05 +### Fixed +- Exception wrapping when error message from the API has not standard format. + ## [1.1.2] - 2022-09-01 ### Added - Link to source code in pypi.org package page diff --git a/oxapi/abstract/api.py b/oxapi/abstract/api.py index 39008ed..ba76520 100644 --- a/oxapi/abstract/api.py +++ b/oxapi/abstract/api.py @@ -159,40 +159,46 @@ def parse_error_message( oxapi.logger.info("Response code: " + str(api_response.status_code)) if api_response.status_code == 200: return - elif api_response.status_code == 401: - self.error = InvalidAPIKeyException( - message=api_response.json()["message"], - headers=api_response.headers, - http_status=api_response.status_code, - http_body=api_response.json(), - ) - elif api_response.status_code == 403: - self.error = NotAllowedException( - message=api_response.json()["message"], - headers=api_response.headers, - http_status=api_response.status_code, - http_body=api_response.json(), - ) - elif api_response.status_code == 404: - self.error = NotFoundException( - message=api_response.json()["message"], - headers=api_response.headers, - http_status=api_response.status_code, - http_body=api_response.json(), - ) else: - self.error = OxAPIError( - message=api_response.json()["message"], - headers=api_response.headers, - http_status=api_response.status_code, - http_body=api_response.json(), - ) + try: + message = api_response.json()["message"] + except KeyError: + try: + message = api_response.json()[list(api_response.json().keys())[0]] + except: + message = api_response.json() + if api_response.status_code == 401: + self.error = InvalidAPIKeyException( + message=message, + headers=api_response.headers, + http_status=api_response.status_code, + http_body=api_response.json(), + ) + elif api_response.status_code == 403: + self.error = NotAllowedException( + message=message, + headers=api_response.headers, + http_status=api_response.status_code, + http_body=api_response.json(), + ) + elif api_response.status_code == 404: + self.error = NotFoundException( + message=message, + headers=api_response.headers, + http_status=api_response.status_code, + http_body=api_response.json(), + ) + else: + self.error = OxAPIError( + message=message, + headers=api_response.headers, + http_status=api_response.status_code, + http_body=api_response.json(), + ) if self.error is not None: if not raise_exceptions: oxapi.logger.warning( - "Request failed: {0}, ERROR: {1}".format( - api_response.url, api_response.json()["message"] - ) + "Request failed: {0}, ERROR: {1}".format(api_response.url, message) ) else: raise self.error diff --git a/setup.py b/setup.py index 9e2a877..928382a 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ long_description = Path("README.md").read_text() NAME = "oxapi" -VERSION = "1.1.2" +VERSION = "1.1.3" DESCRIPTION = "The Python library for querying the OxAPI" AUTHOR = "Oxolo GmbH" AUTHOR_EMAIL = "support@oxapi.ai"