-
Notifications
You must be signed in to change notification settings - Fork 4k
fix: commerce and enrollment apis return 400 when enrollment is not allowed #36612
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
base: master
Are you sure you want to change the base?
Conversation
Thanks for the pull request, @efortish! This repository is currently maintained by Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review. 🔘 Get product approvalIf you haven't already, check this list to see if your contribution needs to go through the product review process.
🔘 Provide contextTo help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:
🔘 Get a green buildIf one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green. Where can I find more information?If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources: When can I expect my changes to be merged?Our goal is to get community contributions seen and reviewed as efficiently as possible. However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:
💡 As a result it may take up to several weeks or months to complete a review and merge your PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @efortish, thanks for this contribution 🙌
I think adding that catch of exception is valuable since the enrollment function may now fail thanks to filters.
My comments in general are minor improvements, and I would like to know if there is a better way to show the message to users.
I still need to test this. Can you clarify the how-to test phase? What steps should I take, and what is the expected result?
@@ -36,6 +36,19 @@ | |||
// If so, redirect to a page explaining to the user | |||
// why they were blocked. | |||
this.redirect(responseData.user_message_url); | |||
} else if (jqXHR.status === 403 && !responseData.user_message_url) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know that the code was status == 403, but again, I think it is better if you define:
const HttpStatus = {
BAD_REQUEST: 400,
FORBIDDEN: 403,
// ... others if you needed it
};
And use jqXHR.status === HttpStatus.FORBIDDEN
to avoid magic numbers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dcoa, is this a good practice? Because it is common in the backend, but I was trying to find examples in other .js files in the edx platform and didn't find a good one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whether or not it is commonly used in frontends doesn't change the fact that it is best practice as a general rule. For the most part the legacy JavaScript in edx-platform
is quite old, and our modern conceptions of best practice would be in the micro frontends, where we do exactly this:
export const ErrorStatuses = {
badRequest: 400,
unauthorized: 401,
forbidden: 403,
notFound: 404,
conflict: 409,
serverError: 500,
};
Summary
This PR improves both backend and frontend handling of enrollment errors, ensuring users receive clear feedback and the system returns appropriate HTTP responses when enrollment is not allowed.
Objective
Technical Details
Backend (
views.py
in enrollments and commerce/api/v0)InvalidEnrollmentAttribute
./api/enrollment/v1/enrollment
and/api/commerce/v0/baskets/
endpoints now provide coherent and user-friendly error responses.Frontend (
enrollment.js
)Test cases and instructions:
The 500 error occurred when making a POST request to these endpoints when enrollment is not allowed:
/api/enrollment/v1/enrollment
and/api/commerce/v0/baskets/
, now it drops 400 error insteadWhen using:
http://apps.local.edly.io:1999/authn/login?course_id=<course_name>&enrollment_action=enroll
, an spinner and a message are rendered and remains 6 seconds before redirect when attempting to enroll when it is not allowed.2025-05-09.14-29-48.mp4
Impact