Release 1.143.2#3420
Conversation
…ate programs (#3418) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Chris Chudzicki <christopher.chudzicki@gmail.com>
OpenAPI ChangesShow/hide ## Changes for v0.yaml:Unexpected changes? Ensure your branch is up-to-date with |
| if len(enrollments) == 0: | ||
| raise EnrollmentCreationFailedError |
There was a problem hiding this comment.
Bug: The custom EnrollmentCreationFailedError is not handled, causing an unhandled exception and a 500 error instead of a graceful API response.
Severity: HIGH
Suggested Fix
Modify EnrollmentCreationFailedError to inherit from rest_framework.exceptions.APIException. Set its status_code to an appropriate value, such as 400_BAD_REQUEST or 404_NOT_FOUND, to ensure DRF's exception handler automatically converts it into a proper HTTP response.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: courses/views/v2/__init__.py#L719-L720
Potential issue: The custom exception `EnrollmentCreationFailedError` inherits from
Python's base `Exception` rather than a DRF `APIException`. When this error is raised,
for instance when an audit enrollment creation fails, it is not caught by any
`try...except` blocks. The custom DRF exception handler delegates to the default
handler, which does not process non-`APIException` types. This results in an unhandled
exception, causing the API to return an HTTP 500 Internal Server Error to the client
instead of a more appropriate 4xx error with a meaningful message, violating the API's
error handling contract.
Did we get this right? 👍 / 👎 to inform future reviews.
| # Learner already has a matching enrollment, so nothing to do. | ||
| return Response(status=status.HTTP_204_NO_CONTENT) | ||
|
|
||
| run = CourseRun.objects.filter(courseware_id=courserun_id).get() |
There was a problem hiding this comment.
Bug: A CourseRun.DoesNotExist exception is not caught when querying for a course run, leading to an unhandled exception and a 500 error.
Severity: HIGH
Suggested Fix
Wrap the CourseRun.objects.get() call in a try...except CourseRun.DoesNotExist block. In the except block, raise a rest_framework.exceptions.NotFound exception with a descriptive message to ensure a proper 404 response is returned to the client.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: courses/views/v2/__init__.py#L679
Potential issue: A call to `CourseRun.objects.get()` is made without wrapping it in a
`try...except` block to handle the `CourseRun.DoesNotExist` exception. If an API request
is made with a `courserun_id` that does not exist in the database, this line will raise
an unhandled `DoesNotExist` exception. Neither the custom exception handler nor the
default DRF handler is configured to convert this specific exception into a 404
response. Consequently, a request with an invalid ID will cause an HTTP 500 Internal
Server Error instead of the expected 404 Not Found.
Did we get this right? 👍 / 👎 to inform future reviews.
James Kachel