Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: updated client-server exception handling #16

Merged
merged 5 commits into from Aug 23, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 21 additions & 15 deletions source/ftrack_api/session.py
Expand Up @@ -1693,36 +1693,42 @@ def call(self, data):
self.logger.debug(L('Calling server {0} with {1!r}', url, data))

try:
result = ''
response = self._request.post(
url,
headers=headers,
data=data,
timeout=self.request_timeout,
)
self.logger.debug(L('Call took: {0}', response.elapsed.total_seconds()))
self.logger.debug(L('Response: {0!r}', response.text))

result = self.decode(response.text)
response.raise_for_status()

except requests.exceptions.HTTPError:
error_message = (
'Server reported error: {0}'
.format(response.text)
)
self._raise_server_error(error_message)

self.logger.debug(L('Call took: {0}', response.elapsed.total_seconds()))
self.logger.debug(L('Response: {0!r}', response.text))
# handle response exceptions and / or other http exceptions
# (strict api used => status code returned => raise_for_status() => HTTPError)
except requests.exceptions.HTTPError as exc:
if 'exception' in result:
error_message = 'Server reported error: {0}({1})'.format(
result['exception'], result['content']
)
self._raise_server_error(error_message)
else:
self._raise_server_error(str(exc))

try:
result = self.decode(response.text)
except Exception:
# JSON response decoding exception
except (TypeError, ValueError):
error_message = (
'Server reported error in unexpected format. Raw error was: {0}'
.format(response.text)
)
self._raise_server_error(error_message)


# handle possible response exceptions
# (strict api not used => 200 returned)
else:
if 'exception' in result:
# Handle exceptions.
if 'exception' in result:
error_message = 'Server reported error: {0}({1})'.format(
result['exception'], result['content']
)
Expand Down