Skip to content

Commit d1f9982

Browse files
authored
Capture all 400/409 errors from openedx (#2974)
1 parent 967ab79 commit d1f9982

File tree

3 files changed

+22
-34
lines changed

3 files changed

+22
-34
lines changed

openedx/api.py

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
EdxApiUserUpdateError,
4343
NoEdxApiAuthError,
4444
OpenEdXOAuth2Error,
45-
OpenEdxUserCreateError,
4645
OpenEdxUserMissingError,
4746
UnknownEdxApiEmailSettingsException,
4847
UnknownEdxApiEnrollException,
@@ -113,19 +112,11 @@ def _is_duplicate_username_error(resp, data):
113112
)
114113

115114

116-
def _is_duplicate_email_username_error(resp, data):
117-
"""Check if the response indicates a duplicate username error."""
118-
return (
119-
resp.status_code == status.HTTP_409_CONFLICT
120-
and data.get("error_code") == "duplicate-email-username"
121-
)
122-
123-
124-
def _is_bad_request(resp, data):
115+
def _is_bad_request(resp):
125116
"""Check if the response indicates a bad request."""
126-
return (
127-
resp.status_code == status.HTTP_400_BAD_REQUEST
128-
or _is_duplicate_email_username_error(resp, data)
117+
return resp.status_code in (
118+
status.HTTP_400_BAD_REQUEST,
119+
status.HTTP_409_CONFLICT,
129120
)
130121

131122

@@ -265,7 +256,7 @@ def _set_edx_error(open_edx_user, data):
265256
open_edx_user.save()
266257

267258

268-
def _create_edx_user_request(open_edx_user, user, access_token): # noqa: C901
259+
def _create_edx_user_request(open_edx_user, user, access_token):
269260
"""
270261
Handle the actual user creation request to Open edX with retry logic for duplicate usernames.
271262
@@ -276,9 +267,6 @@ def _create_edx_user_request(open_edx_user, user, access_token): # noqa: C901
276267
277268
Returns:
278269
bool: True if user was created successfully, False otherwise
279-
280-
Raises:
281-
OpenEdxUserCreateError: if user creation fails
282270
"""
283271
req_session = requests.Session()
284272
if settings.MITX_ONLINE_REGISTRATION_ACCESS_TOKEN is not None:
@@ -316,6 +304,7 @@ def _create_edx_user_request(open_edx_user, user, access_token): # noqa: C901
316304

317305
try:
318306
resp = None
307+
data = None
319308

320309
while attempt < max_attempts:
321310
attempt += 1
@@ -353,18 +342,15 @@ def _create_edx_user_request(open_edx_user, user, access_token): # noqa: C901
353342
if should_reset_attempts:
354343
attempt = 0
355344
continue
356-
elif _is_bad_request(resp, data):
357-
_set_edx_error(open_edx_user, data)
358-
return False
359345
else:
360346
break
361347

362-
if attempt >= max_attempts:
363-
log.error("Failed to create Open edX user after %d attempts.", max_attempts)
348+
if _is_bad_request(resp):
349+
# this is a known type of error dependent on user input
350+
_set_edx_error(open_edx_user, data)
351+
return False
364352

365-
raise OpenEdxUserCreateError(
366-
f"Error creating Open edX user. {get_error_response_summary(resp)}" # noqa: EM102
367-
)
353+
resp.raise_for_status()
368354
finally:
369355
lock.release()
370356

openedx/api_test.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,21 @@ def test_create_edx_user( # noqa: PLR0913
234234

235235
@responses.activate
236236
@pytest.mark.usefixtures("application")
237-
def test_create_edx_user_dupe_email_username(settings):
237+
@pytest.mark.parametrize(
238+
"error_data",
239+
[
240+
{"error_code": "duplicate-email"},
241+
{
242+
"error_code": "duplicate-email-username",
243+
"username_suggestions": [],
244+
},
245+
],
246+
)
247+
def test_create_edx_user_409_errors(settings, error_data):
238248
"""Test that create_edx_user handles a 409 response from the edX API"""
239249
user = UserFactory.create(
240250
openedx_user__has_been_synced=False,
241251
)
242-
error_data = {
243-
"error_code": "duplicate-email-username",
244-
"username_suggestions": [],
245-
}
246252

247253
resp1 = responses.add(
248254
responses.GET,

openedx/exceptions.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
from mitol.common.utils import get_error_response_summary
44

55

6-
class OpenEdxUserCreateError(Exception):
7-
"""Exception creating the OpenEdxUser"""
8-
9-
106
class OpenEdXOAuth2Error(Exception):
117
"""We were unable to obtain a refresh token from openedx"""
128

0 commit comments

Comments
 (0)