Skip to content

Commit

Permalink
Merge pull request #80 from CS3219-AY2425S1/titus/fix-frontend-error-…
Browse files Browse the repository at this point in the history
…handling

fix: error handling and history pagination bug
  • Loading branch information
chiaryan authored Nov 13, 2024
2 parents 9f5833b + 4b971e6 commit 368fa90
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 18 deletions.
5 changes: 4 additions & 1 deletion apps/frontend/__tests__/unit-tests/question.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,11 @@ describe("GetSingleQuestion", () => {
const DOCREF = "mockdocref";
beforeEach(() => {
global.fetch = jest.fn().mockResolvedValue({
ok: true, // Ensure `ok` is true to hit the success branch
async json() {
return QUESTIONS[0]
}
},
text: () => Promise.resolve('mocked response'),
});
});

Expand All @@ -238,6 +240,7 @@ describe("CreateQuestion", () => {
global.fetch = jest.fn().mockResolvedValue({
status: 200,
statusText: "OK",
ok: true, // Ensure `ok` is true to hit the success branch
async json() {
return createdQuestion
}
Expand Down
17 changes: 14 additions & 3 deletions apps/frontend/src/app/collaboration/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ export default function CollaborationPage(props: CollaborationProps) {
});
};

const errorMessage = (message: string) => {
messageApi.open({
type: "error",
content: message,
});
};

const sendSubmissionResultsToMatchedUser = (data: SubmissionResults) => {
if (!providerRef.current) {
throw new Error("Provider not initialized");
Expand Down Expand Up @@ -299,9 +306,13 @@ export default function CollaborationPage(props: CollaborationProps) {
setDescription(data.description);
});

GetVisibleTests(questionDocRefId).then((data: Test[]) => {
setVisibleTestCases(data);
});
GetVisibleTests(questionDocRefId)
.then((data: Test[]) => {
setVisibleTestCases(data);
})
.catch((e) => {
errorMessage(e.message);
});

// Start stopwatch
startStopwatch();
Expand Down
15 changes: 11 additions & 4 deletions apps/frontend/src/app/question/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default function QuestionPage() {
// Message States
const [messageApi, contextHolder] = message.useMessage();

const error = (message: string) => {
const errorMessage = (message: string) => {
messageApi.open({
type: "error",
content: message,
Expand Down Expand Up @@ -129,13 +129,20 @@ export default function QuestionPage() {
setCategories(data.categories);
setDescription(data.description);
})
.catch((e) => {
errorMessage(e.message);
})
.finally(() => {
setIsLoading(false);
});

GetVisibleTests(questionDocRefId).then((data: Test[]) => {
setVisibleTestCases(data);
});
GetVisibleTests(questionDocRefId)
.then((data: Test[]) => {
setVisibleTestCases(data);
})
.catch((e) => {
errorMessage(e.message);
});
}, [questionDocRefId]);

useEffect(() => {
Expand Down
8 changes: 4 additions & 4 deletions apps/frontend/src/app/services/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ export const GetVisibleTests = async (
}
);

if (response.status === 200) {
if (response.ok) {
return response.json();
} else {
throw new Error(
`Error fetching test cases: ${response.status} ${response.statusText}`
`Error fetching test cases: ${await response.text()}`
);
}
}
Expand All @@ -92,7 +92,7 @@ export const ExecuteVisibleAndCustomTests = async (
}
);

if (response.status === 200) {
if (response.ok) {
return response.json();
} else {
throw new Error(
Expand All @@ -116,7 +116,7 @@ export const ExecuteVisibleAndHiddenTestsAndSubmit = async (
}
);

if (response.status === 200) {
if (response.ok) {
return response.json();
} else {
throw new Error(
Expand Down
13 changes: 9 additions & 4 deletions apps/frontend/src/app/services/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,13 @@ export const GetSingleQuestion = async (docRef: string): Promise<Question> => {
}
}
);
const data = await response.json();
return data;
if (response.ok) {
return response.json();
} else {
throw new Error(
`Error reading question: ${await response.text()}`
);
}
};

// Upload single question (TODO: Sean)
Expand All @@ -115,7 +120,7 @@ export const CreateQuestion = async (
body: JSON.stringify(question),
});

if (response.status === 200) {
if (response.ok) {
return response.json();
} else {
throw new Error(
Expand All @@ -140,7 +145,7 @@ export const EditQuestion = async (
}
);

if (response.status === 200) {
if (response.ok) {
return response.json();
} else {
throw new Error(
Expand Down
4 changes: 2 additions & 2 deletions apps/history-service/models/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ func PaginateResponse(limit, offset int, histories []SubmissionHistory) *Histori
end := offset + limit

var paginatedHistory []SubmissionHistory
if start < len(histories) {
if end > len(histories) {
if start <= len(histories) {
if end >= len(histories) {
end = len(histories)
}
} else {
Expand Down

0 comments on commit 368fa90

Please sign in to comment.