Skip to content

Commit 7f54242

Browse files
committed
fix: improve error handling in extended profile form processing
1 parent ac6c456 commit 7f54242

File tree

1 file changed

+20
-16
lines changed
  • openedx/core/djangoapps/user_api/accounts

1 file changed

+20
-16
lines changed

openedx/core/djangoapps/user_api/accounts/api.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,9 @@ def _get_extended_profile_form_instance(
272272
user (User): User instance to associate with the extended profile
273273
field_errors (dict): Dictionary to collect validation errors if form creation fails
274274
275+
Raises:
276+
AccountUpdateError: If there is an error creating the extended profile form
277+
275278
Returns:
276279
Optional[forms.Form]: Extended profile form instance with user data, or None if
277280
no extended profile form is configured or creation fails
@@ -296,12 +299,7 @@ def _get_extended_profile_form_instance(
296299
logger.warning("Extended profile model not available: %s", str(e))
297300
return None
298301
except Exception as e: # pylint: disable=broad-exception-caught
299-
logger.error("Unexpected error creating custom form for user %s: %s", user.username, str(e))
300-
field_errors["extended_profile"] = {
301-
"developer_message": f"Error creating custom form: {str(e)}",
302-
"user_message": _("There was an error processing the extended profile information"),
303-
}
304-
return None
302+
raise AccountUpdateError(f"Error creating custom form: {str(e)}") from e
305303

306304

307305
def _validate_extended_profile_form_and_collect_errors(extended_profile_form: forms.Form, field_errors: dict) -> None:
@@ -311,17 +309,23 @@ def _validate_extended_profile_form_and_collect_errors(extended_profile_form: fo
311309
Args:
312310
extended_profile_form (forms.Form): The extended profile form to validate
313311
field_errors (dict): Dictionary to collect validation errors
314-
"""
315-
if not extended_profile_form.is_valid():
316-
logger.info("Extended profile form validation failed with errors: %s", extended_profile_form.errors)
317312
318-
for field_name, field_errors_list in extended_profile_form.errors.items():
319-
first_error = field_errors_list[0] if field_errors_list else "Unknown error"
320-
321-
field_errors[field_name] = {
322-
"developer_message": f"Error in extended profile field {field_name}: {first_error}",
323-
"user_message": str(first_error),
324-
}
313+
Raises:
314+
AccountUpdateError: If there is an error validating the extended profile form
315+
"""
316+
try:
317+
if not extended_profile_form.is_valid():
318+
logger.info("Extended profile form validation failed with errors: %s", extended_profile_form.errors)
319+
320+
for field_name, field_errors_list in extended_profile_form.errors.items():
321+
first_error = field_errors_list[0] if field_errors_list else "Unknown error"
322+
323+
field_errors[field_name] = {
324+
"developer_message": f"Error in extended profile field {field_name}: {first_error}",
325+
"user_message": str(first_error),
326+
}
327+
except Exception as error: # pylint: disable=broad-exception-caught
328+
raise AccountUpdateError(f"Error thrown when validating extended profile form: '{str(error)}'") from error
325329

326330

327331
def _validate_read_only_fields(user, data, field_errors):

0 commit comments

Comments
 (0)