CS3219-AY2425S1 / cs3219-ay2425s1-project-g34

nus-cs3219-ay2425s1-cs3219-ay2425s1-project-project-template created by GitHub Classroom
MIT License
2 stars 1 forks source link

mismatch backend error handling for adding question #16

Open ChillinRage opened 4 hours ago

ChillinRage commented 4 hours ago

Problem

image

Front end manages to handle the error: image

Back end crashed: image

Possible error

Error stems from validateQuestionFields and createQuestion in question-controller.js. createQuestion expects validateQuestionFields to return undefined/void for invalid fields, but validateQuestionFields will always return a response object for invalid fields instead, causing the !validation check to always be false regardless of the invalid fields. Then the error will persist to the next line, setting every field to undefined.

Proposed fix

I think that field validation should also be done at the front end, so that it can be more specific (to the user) which field is invalid. Otherwise just having the general error message "all fields are required" can be vague/misleading (which in this case would be misleading since the front end accepted my white space input)

On the backend, maybe the validateQuestionFields should not handle error responses, and instead make it abstract to just return a number to signal what kind of errors it is (0 for ok, 1 for topic not being an array, etc.) That way, you can reapply the validate function to update question as well since the front is now passing all the fields for edit as well. There will be another issue in a few minutes about the validation in edit question.