Skip to content

Commit

Permalink
Prevent JSON errors from raising an exception. The exception will be …
Browse files Browse the repository at this point in the history
…logged.
  • Loading branch information
Dave St.Germain committed Feb 22, 2019
1 parent 6e6f579 commit 99e90d8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
9 changes: 7 additions & 2 deletions edx_proctoring/backends/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,5 +338,10 @@ def _make_attempt_request(self, exam, attempt, method='POST', status=None, **pay
if method == 'GET':
headers.update(self._get_language_headers())
log.debug('Making %r attempt request at %r', method, url)
response = self.session.request(method, url, json=payload, headers=headers).json()
return response
response = self.session.request(method, url, json=payload, headers=headers)
try:
data = response.json()
except ValueError:
log.exception("Decoding attempt %r -> %r", attempt, response.content)
data = {}
return data
12 changes: 12 additions & 0 deletions edx_proctoring/backends/tests/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,18 @@ def test_update_exam_attempt_status(self, provider_method_name, corresponding_st
status = getattr(self.provider, provider_method_name)(self.backend_exam['external_id'], attempt_id)
self.assertEqual(status, corresponding_status)

@responses.activate
def test_failed_json(self):
attempt_id = 2
responses.add(
responses.PATCH,
url=self.provider.exam_attempt_url.format(exam_id=self.backend_exam['external_id'], attempt_id=attempt_id),
body='"]'
)
status = self.provider.mark_erroneous_exam_attempt(self.backend_exam['external_id'], attempt_id)
# the important thing is that it didn't raise an exception for bad json
self.assertEqual(status, None)

@responses.activate
def test_remove_attempt(self):
attempt_id = 2
Expand Down

0 comments on commit 99e90d8

Please sign in to comment.