Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Robust Error Handling (User Service) #14

Merged
merged 4 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions backend/question-service/src/routes/questionRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
25 changes: 21 additions & 4 deletions frontend/src/components/QuestionDialog/QuestionDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/services/question.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export default class QuestionService {

static async getQuestions(): Promise<Question[]> {
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(
Expand Down
Loading