diff --git a/courses/views/v1/__init__.py b/courses/views/v1/__init__.py index d5afaa34a3..8881a3bdb2 100644 --- a/courses/views/v1/__init__.py +++ b/courses/views/v1/__init__.py @@ -478,9 +478,22 @@ def partial_update(self, request, *args, **kwargs): # noqa: ARG002 NoEdxApiAuthError, HTTPError, RequestsConnectionError, - ) as exc: - log.exception(str(exc)) # noqa: TRY401 - return Response(data=str(exc), status=status.HTTP_400_BAD_REQUEST) + ): + # Log full details for debugging + log.exception( + "Failed to update email subscription for user %s, enrollment %s, receive_emails=%s", + request.user.id, + enrollment.id, + receive_emails, + ) + + # Return user-friendly error + return Response( + data={ + "error": "Unable to update email preferences at this time. Please try again later or contact support." + }, + status=status.HTTP_400_BAD_REQUEST, + ) else: # only designed to update edx_emails_subscription field # TODO: In the future please add the implementation # noqa: FIX002, TD002, TD003 diff --git a/courses/views/v1/views_test.py b/courses/views/v1/views_test.py index 9a57cde15d..cd7936285f 100644 --- a/courses/views/v1/views_test.py +++ b/courses/views/v1/views_test.py @@ -736,6 +736,10 @@ def test_update_user_enrollment_failure( data={"receive_emails": "on" if receive_emails else ""}, ) assert resp.status_code == status.HTTP_400_BAD_REQUEST + # Update assertion to check for the new generic error message + assert resp.json() == { + "error": "Unable to update email preferences at this time. Please try again later or contact support." + } patched_email_subscription.assert_called_once_with(user, run_enrollment.run) patched_log_exception.assert_called_once()