Skip to content

Commit

Permalink
Merge pull request #30 from Jiayan-Lim/question-ui
Browse files Browse the repository at this point in the history
Change error msg for edit
  • Loading branch information
Jiayan-Lim authored Sep 29, 2024
2 parents 4dcba97 + 7599304 commit 71d86d6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
4 changes: 2 additions & 2 deletions backend/question-service/controllers/questionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ exports.addQuestion = async (req, res) => {
});
}
if (error.code == 11000) {
return res.status(200).json({
return res.status(404).json({
errorCode: "DUPLICATE_TITLE",
msg: "This title already exists."

Expand Down Expand Up @@ -114,7 +114,7 @@ exports.updateQuestion = async (req, res) => {
res.status(200).json(updatedQuestion);
} catch (error) {
if (error.code == 11000) {
return res.status(200).json({
return res.status(404).json({
errorCode: "DUPLICATE_TITLE",
msg: "This title already exists."
});
Expand Down
25 changes: 14 additions & 11 deletions frontend/app/questions/add-edit-question-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
TooltipProvider,
TooltipTrigger,
} from "@/components/ui/tooltip";
import { escape } from "querystring";

interface AddEditQuestionDialogProps {
row: Question | null;
Expand Down Expand Up @@ -139,17 +140,21 @@ function AddEditQuestionDialog(
});

if (!response.ok) {
if (!row?.id) {
console.log("error response from backend: ", response)

const errorResponse = await response.json(); // Get the error details from the response
const errorMessages = errorResponse.errors
? errorResponse.errors.join(", ")
: errorResponse.message || "An unexpected error occurred.";
const errorCode = errorResponse.errorCode;
if (errorCode == "DUPLICATE_TITLE") {
console.log("error")
alert(`Error: Question "${newQuestion.title}" already exists`);
} else {
const errorMessages = errorResponse.errors
? errorResponse.errors.join(", ")
: errorResponse.message || "An unexpected error occurred.";

alert(`Error: ${errorMessages}`);
alert(`Error: ${errorMessages}`);
}
return;
} else {
throw new Error("Failed to update the question to backend");
}
}
const responseText = await response.text();

Expand Down Expand Up @@ -187,9 +192,7 @@ function AddEditQuestionDialog(
handleClose();
} catch (error) {
alert(
`An error occurred while ${row?.id ? "updating" : "creating"} ${
row?.id ? row?.id : ""
} the question. Please try again.`
`An error occurred while ${row?.id ? "updating" : "creating"} the question. Please try again.`
);
console.error(
`Error ${row?.id ? "updating" : "creating"} question:`,
Expand Down

0 comments on commit 71d86d6

Please sign in to comment.