Skip to content

Commit

Permalink
Generate an error code when the attempts limit has been reached
Browse files Browse the repository at this point in the history
Now that we have a better error message for submissions related issues
emit a known error code for these instead of silently not generating a
submission.
  • Loading branch information
marcospri committed Jun 25, 2024
1 parent 9b4feee commit 4c58c04
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
1 change: 1 addition & 0 deletions lms/error_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ class ErrorCode(str, Enum):
)
REUSED_CONSUMER_KEY = "reused_consumer_key"
CANVAS_SUBMISSION_COURSE_NOT_AVAILABLE = "canvas_submission_course_not_available"
CANVAS_SUBMISSION_MAX_ATTEMPTS = "canvas_submission_max_attempts"
9 changes: 0 additions & 9 deletions lms/services/lti_grading/_v13.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,6 @@ def record_result(self, grading_id, score=None, pre_record_hook=None, comment=No
)

except ExternalRequestError as err:
if (
err.status_code == 422
and "maximum number of allowed attempts has been reached"
in err.response.text
):
LOG.error("record_result: maximum number of allowed attempts")
# We silently shallow this type of error
return None

for expected_code, expected_text in [
# Blackboard
(400, "User could not be found:"),
Expand Down
10 changes: 10 additions & 0 deletions lms/views/api/grading.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ def record_canvas_speedgrader_submission(self):
)

except ExternalRequestError as err:
if (
err.status_code == 422
# This is LTI1.3 only. In LTI1.1 canvas behaves differently and allows this type of submissions.
and "maximum number of allowed attempts has been reached"
in err.response.text
):
raise SerializableError(
error_code=ErrorCode.CANVAS_SUBMISSION_MAX_ATTEMPTS
) from err

# This is Canvas only, we do the error handling here instead of the view to avoid
# conflicting different, more general use cases.
# This is what we get with the Course Participation end date has passed.
Expand Down
12 changes: 0 additions & 12 deletions tests/unit/lms/services/lti_grading/_v13_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,6 @@ def test_record_result_raises_StudentNotInCourse(
with pytest.raises(StudentNotInCourse):
svc.record_result(sentinel.user_id, sentinel.score)

def test_record_result_doesnt_raise_max_submissions(self, svc, ltia_http_service):
ltia_http_service.request.side_effect = ExternalRequestError(
response=Mock(
status_code=422,
text="maximum number of allowed attempts has been reached",
)
)

response = svc.record_result(sentinel.user_id, sentinel.score)

assert not response

def test_create_line_item(self, svc, ltia_http_service):
response = svc.create_line_item(
sentinel.resource_link_id,
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/lms/views/api/grading_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ def test_it_does_not_record_result_if_score_already_exists(

lti_grading_service.record_result.assert_not_called()

def test_it_max_attempts(self, pyramid_request, lti_grading_service):
lti_grading_service.read_result.side_effect = ExternalRequestError(
response=Mock(
status_code=422,
text="maximum number of allowed attempts has been reached",
)
)

with pytest.raises(SerializableError):
GradingViews(pyramid_request).record_canvas_speedgrader_submission()

@pytest.mark.parametrize(
"error_message",
["Course not available for students", "This course has concluded"],
Expand Down

0 comments on commit 4c58c04

Please sign in to comment.