From 6067412d5d9488da3cd511583d2b2596a3353bb1 Mon Sep 17 00:00:00 2001 From: tituschewxj Date: Wed, 13 Nov 2024 00:00:23 +0800 Subject: [PATCH 1/7] fix: error handling on frontend --- .../src/app/collaboration/[id]/page.tsx | 17 ++++++++++++++--- apps/frontend/src/app/question/[id]/page.tsx | 15 +++++++++++---- apps/frontend/src/app/services/question.ts | 13 +++++++++---- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/apps/frontend/src/app/collaboration/[id]/page.tsx b/apps/frontend/src/app/collaboration/[id]/page.tsx index 739eac08a6..2706be0cd6 100644 --- a/apps/frontend/src/app/collaboration/[id]/page.tsx +++ b/apps/frontend/src/app/collaboration/[id]/page.tsx @@ -168,6 +168,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"); @@ -298,9 +305,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/question.ts b/apps/frontend/src/app/services/question.ts index 833b8fc558..905a5c2c91 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: ${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( From 471b7c7e3e9255a6a834f79bab6002649575ff9b Mon Sep 17 00:00:00 2001 From: tituschewxj Date: Wed, 13 Nov 2024 00:00:35 +0800 Subject: [PATCH 2/7] fix: history service pagination --- apps/history-service/models/pagination.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 { From 75804d5e03f9250dd24e4850fcce40468a9ff241 Mon Sep 17 00:00:00 2001 From: tituschewxj Date: Wed, 13 Nov 2024 00:04:32 +0800 Subject: [PATCH 3/7] fix: use response.text instead --- apps/frontend/src/app/services/execute.ts | 8 ++++---- apps/frontend/src/app/services/question.ts | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) 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 905a5c2c91..98c10be481 100644 --- a/apps/frontend/src/app/services/question.ts +++ b/apps/frontend/src/app/services/question.ts @@ -102,7 +102,7 @@ export const GetSingleQuestion = async (docRef: string): Promise => { return response.json(); } else { throw new Error( - `Error reading question: ${response.text}` + `Error reading question: ${await response.text()}` ); } }; From 68b64ac9edc520c8577be12ae4c115aa124f6c3a Mon Sep 17 00:00:00 2001 From: tituschewxj Date: Wed, 13 Nov 2024 00:33:23 +0800 Subject: [PATCH 4/7] fix: update tests to pass --- apps/frontend/__tests__/question.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/frontend/__tests__/question.test.ts b/apps/frontend/__tests__/question.test.ts index 417b4cc7aa..dc3f166bc0 100644 --- a/apps/frontend/__tests__/question.test.ts +++ b/apps/frontend/__tests__/question.test.ts @@ -212,7 +212,8 @@ describe("GetSingleQuestion", () => { global.fetch = jest.fn().mockResolvedValue({ async json() { return QUESTIONS[0] - } + }, + text: () => Promise.resolve('mocked response'), }); }); From 408165423204d05dbf192dd3a6949c08fa90601c Mon Sep 17 00:00:00 2001 From: tituschewxj Date: Wed, 13 Nov 2024 00:38:00 +0800 Subject: [PATCH 5/7] fix: fix test --- apps/frontend/__tests__/question.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/frontend/__tests__/question.test.ts b/apps/frontend/__tests__/question.test.ts index dc3f166bc0..aad4890726 100644 --- a/apps/frontend/__tests__/question.test.ts +++ b/apps/frontend/__tests__/question.test.ts @@ -213,7 +213,9 @@ describe("GetSingleQuestion", () => { async json() { return QUESTIONS[0] }, - text: () => Promise.resolve('mocked response'), + async text() { + return 'mocked response' + } }); }); From 540f23f2b6ae58c1b622825e1d59dcfc32604a21 Mon Sep 17 00:00:00 2001 From: tituschewxj Date: Wed, 13 Nov 2024 00:40:18 +0800 Subject: [PATCH 6/7] fix: fix test --- apps/frontend/__tests__/question.test.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/apps/frontend/__tests__/question.test.ts b/apps/frontend/__tests__/question.test.ts index aad4890726..f5bf7e5ff6 100644 --- a/apps/frontend/__tests__/question.test.ts +++ b/apps/frontend/__tests__/question.test.ts @@ -210,12 +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] }, - async text() { - return 'mocked response' - } + text: () => Promise.resolve('mocked response'), }); }); From 86114a72abd4eccdf4b48a85bb2b57394d2f8981 Mon Sep 17 00:00:00 2001 From: tituschewxj Date: Wed, 13 Nov 2024 00:44:33 +0800 Subject: [PATCH 7/7] fix(tests): put ok in mocked fetch fn --- apps/frontend/__tests__/question.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/frontend/__tests__/question.test.ts b/apps/frontend/__tests__/question.test.ts index f5bf7e5ff6..ca53f7365e 100644 --- a/apps/frontend/__tests__/question.test.ts +++ b/apps/frontend/__tests__/question.test.ts @@ -210,7 +210,7 @@ describe("GetSingleQuestion", () => { const DOCREF = "mockdocref"; beforeEach(() => { global.fetch = jest.fn().mockResolvedValue({ - ok: true, // // Ensure `ok` is true to hit the success branch + ok: true, // Ensure `ok` is true to hit the success branch async json() { return QUESTIONS[0] }, @@ -240,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 }