Skip to content

Commit ab15d5e

Browse files
committed
fix(emailpassword): Send 400 instead of 500 on invalid request body
1 parent b158b72 commit ab15d5e

File tree

1 file changed

+28
-21
lines changed
  • supertokens_python/recipe/emailpassword/api

1 file changed

+28
-21
lines changed

supertokens_python/recipe/emailpassword/api/utils.py

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -54,28 +54,35 @@ async def validate_form_or_throw_error(
5454
async def validate_form_fields_or_throw_error(
5555
config_form_fields: List[NormalisedFormField], form_fields_raw: Any
5656
) -> List[FormField]:
57-
if form_fields_raw is None:
58-
raise_bad_input_exception("Missing input param: formFields")
57+
try:
58+
if form_fields_raw is None:
59+
raise_bad_input_exception("Missing input param: formFields")
5960

60-
if not isinstance(form_fields_raw, List):
61-
raise_bad_input_exception("formFields must be an array")
61+
if not isinstance(form_fields_raw, List):
62+
raise_bad_input_exception("formFields must be an array")
6263

63-
form_fields: List[FormField] = []
64+
form_fields: List[FormField] = []
6465

65-
form_fields_list_raw: List[Dict[str, Any]] = form_fields_raw
66-
for current_form_field in form_fields_list_raw:
67-
if (
68-
"id" not in current_form_field
69-
or not isinstance(current_form_field["id"], str)
70-
or "value" not in current_form_field
71-
):
72-
raise_bad_input_exception(
73-
"All elements of formFields must contain an 'id' and 'value' field"
74-
)
75-
value = current_form_field["value"]
76-
if current_form_field["id"] == FORM_FIELD_EMAIL_ID:
77-
value = value.strip()
78-
form_fields.append(FormField(current_form_field["id"], value))
66+
form_fields_list_raw: List[Dict[str, Any]] = form_fields_raw
67+
for current_form_field in form_fields_list_raw:
68+
if (
69+
"id" not in current_form_field
70+
or not isinstance(current_form_field["id"], str)
71+
or "value" not in current_form_field
72+
):
73+
raise_bad_input_exception(
74+
"All elements of formFields must contain an 'id' and 'value' field"
75+
)
76+
value = current_form_field["value"]
77+
if current_form_field["id"] == FORM_FIELD_EMAIL_ID:
78+
if not isinstance(value, str):
79+
raise_bad_input_exception("email value must be a string")
80+
value = value.strip()
81+
form_fields.append(FormField(current_form_field["id"], value))
7982

80-
await validate_form_or_throw_error(form_fields, config_form_fields)
81-
return form_fields
83+
await validate_form_or_throw_error(form_fields, config_form_fields)
84+
return form_fields
85+
except Exception:
86+
raise_bad_input_exception(
87+
"Something seems wrong with the input formFields. Please check the request body."
88+
)

0 commit comments

Comments
 (0)