Skip to content

Commit 362c665

Browse files
ks129jb3
andauthored
Make JSON errors more JS friendly and add response codes
Co-authored-by: Joe Banks <[email protected]>
1 parent d23f3c0 commit 362c665

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

backend/routes/forms/new.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async def post(self, request: Request) -> JSONResponse:
2929
if await request.state.db.forms.find_one({"_id": form.id}):
3030
return JSONResponse({
3131
"error": "Form with same ID already exists."
32-
})
32+
}, status_code=400)
3333

3434
await request.state.db.forms.insert_one(form.dict(by_alias=True))
3535
return JSONResponse(form.dict())

backend/routes/forms/submit.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ async def post(self, request: Request) -> JSONResponse:
7979

8080
if FormFeatures.COLLECT_EMAIL.value in form.features and "email" not in response["user"]: # noqa
8181
return JSONResponse({
82-
"error": "User data doesn't include email."
83-
})
82+
"error": "email_required"
83+
}, status_code=400)
8484
else:
8585
return JSONResponse({
86-
"error": "Missing Discord user data"
87-
})
86+
"error": "missing_discord_data"
87+
}, status_code=400)
8888

8989
missing_fields = []
9090
for question in form.questions:
@@ -93,8 +93,9 @@ async def post(self, request: Request) -> JSONResponse:
9393

9494
if missing_fields:
9595
return JSONResponse({
96-
"error": f"Following missing fields: {', '.join(missing_fields)}."
97-
})
96+
"error": "missing_fields",
97+
"fields": missing_fields
98+
}, status_code=400)
9899

99100
try:
100101
response_obj = FormResponse(**response)

0 commit comments

Comments
 (0)