diff --git a/backend/question-service/src/routes/questionRoutes.ts b/backend/question-service/src/routes/questionRoutes.ts index c188c307b8..33a5abf67d 100644 --- a/backend/question-service/src/routes/questionRoutes.ts +++ b/backend/question-service/src/routes/questionRoutes.ts @@ -30,7 +30,7 @@ router.post("/", async (req: Request, res: Response) => { router.get("/", async (req: Request, res: Response) => { try { const questions = await Question.find(); - res.json(questions); + res.status(200).json(questions); } catch (err) { res .status(500) @@ -46,7 +46,7 @@ router.get("/:id", async (req: Request, res: Response) => { questionId: id, }); if (!question) return res.status(404).json({ error: "Question not found" }); - res.json(question); + res.status(200).json(question); } catch (err) { res .status(500) @@ -67,7 +67,7 @@ router.put("/:id", async (req: Request, res: Response) => { ); if (!updatedQuestion) return res.status(404).json({ error: "Question not found" }); - res.json(updatedQuestion); + res.status(200).json(updatedQuestion); } catch (err) { res .status(500) @@ -84,7 +84,7 @@ router.delete("/:id", async (req: Request, res: Response) => { }); if (!deletedQuestion) return res.status(404).json({ error: "Question not found" }); - res.json({ message: "Question deleted successfully" }); + res.status(200).json({ message: "Question deleted successfully" }); } catch (err) { res .status(500) diff --git a/frontend/src/components/QuestionDialog/QuestionDialog.tsx b/frontend/src/components/QuestionDialog/QuestionDialog.tsx index 6b5795c57b..473dfb89f1 100644 --- a/frontend/src/components/QuestionDialog/QuestionDialog.tsx +++ b/frontend/src/components/QuestionDialog/QuestionDialog.tsx @@ -72,13 +72,30 @@ const QuestionDialog = (props: { questionCallback(response, isAddNew ? "added" : "updated"); closeMainDialog(); } catch (error: any) { - let errorMessage = "An unknown error occurred"; if (error instanceof QuestionValidationError) { - errorMessage = error.message; + setError(error.message); + } else if (error.response) { + switch (error.response.status) { + case 400: + setError(`Question with questionId - ${id} already exists`); + break; + case 404: + setError("Question not found"); + break; + case 401: + setError("Unauthorized - please log in"); + break; + case 403: + setError("Forbidden - you don't have permission"); + break; + default: + setError(`Server error: ${error.response.status}`); + } + } else if (error.request) { + setError("No response from server"); } else { - errorMessage = error.response.data.message; + setError("Unknown Error Occured"); } - setError(errorMessage); setIsErrorDisplayed(true); } }; diff --git a/frontend/src/services/question.service.ts b/frontend/src/services/question.service.ts index 8bbbe33b4b..800a87fb64 100644 --- a/frontend/src/services/question.service.ts +++ b/frontend/src/services/question.service.ts @@ -12,7 +12,8 @@ export default class QuestionService { static async getQuestions(): Promise { const response = await QuestionService.client.get("/"); - return response.data; + let questions = response.data.sort((a: Question, b: Question) => a.questionId - b.questionId); + return questions; } static async addQuestion(