Skip to content
This repository was archived by the owner on Aug 8, 2024. It is now read-only.

Commit 586cb09

Browse files
authored
Merge pull request #14 from Oxolo/bug-fix-oxapierror
Bug oxapi error fixed
2 parents 1dad563 + dbcd080 commit 586cb09

File tree

3 files changed

+41
-31
lines changed

3 files changed

+41
-31
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file.
33

44
<!--- ## [1.x.x] - 2022-xx-xx --->
55

6+
## [1.1.3] - 2022-09-05
7+
### Fixed
8+
- Exception wrapping when error message from the API has not standard format.
9+
610
## [1.1.2] - 2022-09-01
711
### Added
812
- Link to source code in pypi.org package page

oxapi/abstract/api.py

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -159,40 +159,46 @@ def parse_error_message(
159159
oxapi.logger.info("Response code: " + str(api_response.status_code))
160160
if api_response.status_code == 200:
161161
return
162-
elif api_response.status_code == 401:
163-
self.error = InvalidAPIKeyException(
164-
message=api_response.json()["message"],
165-
headers=api_response.headers,
166-
http_status=api_response.status_code,
167-
http_body=api_response.json(),
168-
)
169-
elif api_response.status_code == 403:
170-
self.error = NotAllowedException(
171-
message=api_response.json()["message"],
172-
headers=api_response.headers,
173-
http_status=api_response.status_code,
174-
http_body=api_response.json(),
175-
)
176-
elif api_response.status_code == 404:
177-
self.error = NotFoundException(
178-
message=api_response.json()["message"],
179-
headers=api_response.headers,
180-
http_status=api_response.status_code,
181-
http_body=api_response.json(),
182-
)
183162
else:
184-
self.error = OxAPIError(
185-
message=api_response.json()["message"],
186-
headers=api_response.headers,
187-
http_status=api_response.status_code,
188-
http_body=api_response.json(),
189-
)
163+
try:
164+
message = api_response.json()["message"]
165+
except KeyError:
166+
try:
167+
message = api_response.json()[list(api_response.json().keys())[0]]
168+
except:
169+
message = api_response.json()
170+
if api_response.status_code == 401:
171+
self.error = InvalidAPIKeyException(
172+
message=message,
173+
headers=api_response.headers,
174+
http_status=api_response.status_code,
175+
http_body=api_response.json(),
176+
)
177+
elif api_response.status_code == 403:
178+
self.error = NotAllowedException(
179+
message=message,
180+
headers=api_response.headers,
181+
http_status=api_response.status_code,
182+
http_body=api_response.json(),
183+
)
184+
elif api_response.status_code == 404:
185+
self.error = NotFoundException(
186+
message=message,
187+
headers=api_response.headers,
188+
http_status=api_response.status_code,
189+
http_body=api_response.json(),
190+
)
191+
else:
192+
self.error = OxAPIError(
193+
message=message,
194+
headers=api_response.headers,
195+
http_status=api_response.status_code,
196+
http_body=api_response.json(),
197+
)
190198
if self.error is not None:
191199
if not raise_exceptions:
192200
oxapi.logger.warning(
193-
"Request failed: {0}, ERROR: {1}".format(
194-
api_response.url, api_response.json()["message"]
195-
)
201+
"Request failed: {0}, ERROR: {1}".format(api_response.url, message)
196202
)
197203
else:
198204
raise self.error

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
long_description = Path("README.md").read_text()
1414

1515
NAME = "oxapi"
16-
VERSION = "1.1.2"
16+
VERSION = "1.1.3"
1717
DESCRIPTION = "The Python library for querying the OxAPI"
1818
AUTHOR = "Oxolo GmbH"
1919
AUTHOR_EMAIL = "[email protected]"

0 commit comments

Comments
 (0)