Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion openedx/core/djangoapps/enrollments/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
CourseEnrollmentAttribute,
CourseFullError,
EnrollmentClosedError,
NonExistentCourseError
NonExistentCourseError,
EnrollmentNotAllowed,
)
from common.djangoapps.student.roles import RoleCache

Expand Down Expand Up @@ -148,6 +149,8 @@ def create_course_enrollment(username, course_id, mode, is_active):
try:
enrollment = CourseEnrollment.enroll(user, course_key, check_access=True)
return _update_enrollment(enrollment, is_active=is_active, mode=mode)
except EnrollmentNotAllowed as err:
raise InvalidEnrollmentAttribute(str(err))
except NonExistentCourseError as err:
raise CourseNotFoundError(str(err)) # lint-amnesty, pylint: disable=raise-missing-from
except EnrollmentClosedError as err:
Expand Down
9 changes: 8 additions & 1 deletion openedx/core/djangoapps/enrollments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from openedx.core.djangoapps.embargo import api as embargo_api
from openedx.core.djangoapps.enrollments import api
from openedx.core.djangoapps.enrollments.errors import (
CourseEnrollmentError, CourseEnrollmentExistsError, CourseModeNotFoundError,
CourseEnrollmentError, CourseEnrollmentExistsError, CourseModeNotFoundError, InvalidEnrollmentAttribute,
)
from openedx.core.djangoapps.enrollments.forms import CourseEnrollmentsApiListForm
from openedx.core.djangoapps.enrollments.paginators import CourseEnrollmentsApiListPagination
Expand Down Expand Up @@ -818,6 +818,13 @@ def post(self, request):

log.info('The user [%s] has already been enrolled in course run [%s].', username, course_id)
return Response(response)
except InvalidEnrollmentAttribute as error:
return Response(
status=status.HTTP_400_BAD_REQUEST,
data={
"message": str(error)
}
)
except CourseModeNotFoundError as error:
return Response(
status=status.HTTP_400_BAD_REQUEST,
Expand Down