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

fix: Check for errors before parsing api json response #12

Merged
merged 3 commits into from Apr 5, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 18 additions & 0 deletions .github/workflows/pr.yml
@@ -0,0 +1,18 @@
name: PR

on:
pull_request:
types:
- opened
- edited
- synchronize
- closed
- converted_to_draft
- ready_for_review
jobs:
check-pr:
name: Check PR
uses: ftrackhq/ftrack-actions/.github/workflows/pr-base.yml@main
secrets:
token: ${{ secrets.GITHUB_TOKEN }}
ftrackApiKey: ${{ secrets.FTRACK_API_KEY }}
37 changes: 23 additions & 14 deletions source/ftrack_api/session.py
Expand Up @@ -1692,38 +1692,47 @@ def call(self, data):

self.logger.debug(L('Calling server {0} with {1!r}', url, data))

response = self._request.post(
url,
headers=headers,
data=data,
timeout=self.request_timeout,
)
try:
response = self._request.post(
url,
headers=headers,
data=data,
timeout=self.request_timeout,
)
response.raise_for_status()

self.logger.debug(L('Call took: {0}', response.elapsed.total_seconds()))
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))
try:
result = self.decode(response.text)

try:
result = self.decode(response.text)
except Exception:
error_message = (
'Server reported error in unexpected format. Raw error was: {0}'
.format(response.text)
)
self.logger.exception(error_message)
raise ftrack_api.exception.ServerError(error_message)
self._raise_server_error(error_message)

else:
if 'exception' in result:
# Handle exceptions.
error_message = 'Server reported error: {0}({1})'.format(
result['exception'], result['content']
)
self.logger.exception(error_message)
raise ftrack_api.exception.ServerError(error_message)

self._raise_server_error(error_message)
return result

def _raise_server_error(self, error_message):
self.logger.exception(error_message)
raise ftrack_api.exception.ServerError(error_message)

def encode(self, data, entity_attribute_strategy='set_only'):
'''Return *data* encoded as JSON formatted string.

Expand Down