diff --git a/apps/frontend/__tests__/unit-tests/question.test.ts b/apps/frontend/__tests__/unit-tests/question.test.ts index 66e22635e3..970407f3ec 100644 --- a/apps/frontend/__tests__/unit-tests/question.test.ts +++ b/apps/frontend/__tests__/unit-tests/question.test.ts @@ -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'), }); }); @@ -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 } diff --git a/apps/frontend/src/app/collaboration/[id]/page.tsx b/apps/frontend/src/app/collaboration/[id]/page.tsx index 0eb9721482..ff439397fe 100644 --- a/apps/frontend/src/app/collaboration/[id]/page.tsx +++ b/apps/frontend/src/app/collaboration/[id]/page.tsx @@ -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"); @@ -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(); diff --git a/apps/frontend/src/app/question/[id]/page.tsx b/apps/frontend/src/app/question/[id]/page.tsx index 9b5b353ad4..9ec650f231 100644 --- a/apps/frontend/src/app/question/[id]/page.tsx +++ b/apps/frontend/src/app/question/[id]/page.tsx @@ -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, @@ -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(() => { diff --git a/apps/frontend/src/app/services/execute.ts b/apps/frontend/src/app/services/execute.ts index 12c950bd6b..a0d545c5b9 100644 --- a/apps/frontend/src/app/services/execute.ts +++ b/apps/frontend/src/app/services/execute.ts @@ -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()}` ); } } @@ -92,7 +92,7 @@ export const ExecuteVisibleAndCustomTests = async ( } ); - if (response.status === 200) { + if (response.ok) { return response.json(); } else { throw new Error( @@ -116,7 +116,7 @@ export const ExecuteVisibleAndHiddenTestsAndSubmit = async ( } ); - if (response.status === 200) { + if (response.ok) { return response.json(); } else { throw new Error( diff --git a/apps/frontend/src/app/services/question.ts b/apps/frontend/src/app/services/question.ts index 833b8fc558..98c10be481 100644 --- a/apps/frontend/src/app/services/question.ts +++ b/apps/frontend/src/app/services/question.ts @@ -98,8 +98,13 @@ export const GetSingleQuestion = async (docRef: string): Promise => { } } ); - 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) @@ -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( @@ -140,7 +145,7 @@ export const EditQuestion = async ( } ); - if (response.status === 200) { + if (response.ok) { return response.json(); } else { throw new Error( diff --git a/apps/history-service/models/pagination.go b/apps/history-service/models/pagination.go index 3b9f1d38c3..930f76e656 100644 --- a/apps/history-service/models/pagination.go +++ b/apps/history-service/models/pagination.go @@ -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 {