diff --git a/backend/question-service/src/routes/questionRoutes.ts b/backend/question-service/src/routes/questionRoutes.ts index 3fcca5f200..c188c307b8 100644 --- a/backend/question-service/src/routes/questionRoutes.ts +++ b/backend/question-service/src/routes/questionRoutes.ts @@ -20,7 +20,9 @@ router.post("/", async (req: Request, res: Response) => { const savedQuestion = await newQuestion.save(); res.status(201).json(savedQuestion); } catch (err) { - res.status(400).json({ error: (err as Error).message }); + res.status(400).json({ + message: `Question with questionId ${questionId} already exists`, + }); } }); @@ -30,7 +32,9 @@ router.get("/", async (req: Request, res: Response) => { const questions = await Question.find(); res.json(questions); } catch (err) { - res.status(500).json({ error: (err as Error).message }); + res + .status(500) + .json({ message: "Server Error : Unable to fetch questions Question" }); } }); @@ -44,7 +48,9 @@ router.get("/:id", async (req: Request, res: Response) => { if (!question) return res.status(404).json({ error: "Question not found" }); res.json(question); } catch (err) { - res.status(500).json({ error: (err as Error).message }); + res + .status(500) + .json({ message: "Server Error : Unable to fetch Question" }); } }); @@ -63,7 +69,9 @@ router.put("/:id", async (req: Request, res: Response) => { return res.status(404).json({ error: "Question not found" }); res.json(updatedQuestion); } catch (err) { - res.status(500).json({ error: (err as Error).message }); + res + .status(500) + .json({ message: "Server Error : Unable to update Question" }); } }); @@ -78,7 +86,9 @@ router.delete("/:id", async (req: Request, res: Response) => { return res.status(404).json({ error: "Question not found" }); res.json({ message: "Question deleted successfully" }); } catch (err) { - res.status(500).json({ error: (err as Error).message }); + res + .status(500) + .json({ message: "Server Error : Unable to delete Question" }); } }); diff --git a/frontend/.env.sample b/frontend/.env.sample index 861b536c54..a43d6bb1e4 100644 --- a/frontend/.env.sample +++ b/frontend/.env.sample @@ -1 +1 @@ -port=3001 \ No newline at end of file +PORT=3001 \ No newline at end of file diff --git a/frontend/src/components/QuestionDialog/QuestionDialog.tsx b/frontend/src/components/QuestionDialog/QuestionDialog.tsx index 2b7075f51d..6b5795c57b 100644 --- a/frontend/src/components/QuestionDialog/QuestionDialog.tsx +++ b/frontend/src/components/QuestionDialog/QuestionDialog.tsx @@ -65,18 +65,21 @@ const QuestionDialog = (props: { const handleSubmit = async () => { setIsErrorDisplayed(false); - try { const response = isAddNew ? await QuestionService.addQuestion(id, title, description, categoriesString, complexity, link) : await QuestionService.editQuestion(id, title, description, categoriesString, complexity, link); - closeMainDialog(); questionCallback(response, isAddNew ? "added" : "updated"); + closeMainDialog(); } catch (error: any) { - setError(error?.message ?? "An unknown error occurred"); - setIsErrorDisplayed(true); + let errorMessage = "An unknown error occurred"; if (error instanceof QuestionValidationError) { + errorMessage = error.message; + } else { + errorMessage = error.response.data.message; } + setError(errorMessage); + setIsErrorDisplayed(true); } };