Skip to content

Commit 368fa90

Browse files
authored
Merge pull request #80 from CS3219-AY2425S1/titus/fix-frontend-error-handling
fix: error handling and history pagination bug
2 parents 9f5833b + 4b971e6 commit 368fa90

File tree

6 files changed

+44
-18
lines changed

6 files changed

+44
-18
lines changed

apps/frontend/__tests__/unit-tests/question.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,11 @@ describe("GetSingleQuestion", () => {
210210
const DOCREF = "mockdocref";
211211
beforeEach(() => {
212212
global.fetch = jest.fn().mockResolvedValue({
213+
ok: true, // Ensure `ok` is true to hit the success branch
213214
async json() {
214215
return QUESTIONS[0]
215-
}
216+
},
217+
text: () => Promise.resolve('mocked response'),
216218
});
217219
});
218220

@@ -238,6 +240,7 @@ describe("CreateQuestion", () => {
238240
global.fetch = jest.fn().mockResolvedValue({
239241
status: 200,
240242
statusText: "OK",
243+
ok: true, // Ensure `ok` is true to hit the success branch
241244
async json() {
242245
return createdQuestion
243246
}

apps/frontend/src/app/collaboration/[id]/page.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ export default function CollaborationPage(props: CollaborationProps) {
169169
});
170170
};
171171

172+
const errorMessage = (message: string) => {
173+
messageApi.open({
174+
type: "error",
175+
content: message,
176+
});
177+
};
178+
172179
const sendSubmissionResultsToMatchedUser = (data: SubmissionResults) => {
173180
if (!providerRef.current) {
174181
throw new Error("Provider not initialized");
@@ -299,9 +306,13 @@ export default function CollaborationPage(props: CollaborationProps) {
299306
setDescription(data.description);
300307
});
301308

302-
GetVisibleTests(questionDocRefId).then((data: Test[]) => {
303-
setVisibleTestCases(data);
304-
});
309+
GetVisibleTests(questionDocRefId)
310+
.then((data: Test[]) => {
311+
setVisibleTestCases(data);
312+
})
313+
.catch((e) => {
314+
errorMessage(e.message);
315+
});
305316

306317
// Start stopwatch
307318
startStopwatch();

apps/frontend/src/app/question/[id]/page.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default function QuestionPage() {
3939
// Message States
4040
const [messageApi, contextHolder] = message.useMessage();
4141

42-
const error = (message: string) => {
42+
const errorMessage = (message: string) => {
4343
messageApi.open({
4444
type: "error",
4545
content: message,
@@ -129,13 +129,20 @@ export default function QuestionPage() {
129129
setCategories(data.categories);
130130
setDescription(data.description);
131131
})
132+
.catch((e) => {
133+
errorMessage(e.message);
134+
})
132135
.finally(() => {
133136
setIsLoading(false);
134137
});
135138

136-
GetVisibleTests(questionDocRefId).then((data: Test[]) => {
137-
setVisibleTestCases(data);
138-
});
139+
GetVisibleTests(questionDocRefId)
140+
.then((data: Test[]) => {
141+
setVisibleTestCases(data);
142+
})
143+
.catch((e) => {
144+
errorMessage(e.message);
145+
});
139146
}, [questionDocRefId]);
140147

141148
useEffect(() => {

apps/frontend/src/app/services/execute.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ export const GetVisibleTests = async (
6868
}
6969
);
7070

71-
if (response.status === 200) {
71+
if (response.ok) {
7272
return response.json();
7373
} else {
7474
throw new Error(
75-
`Error fetching test cases: ${response.status} ${response.statusText}`
75+
`Error fetching test cases: ${await response.text()}`
7676
);
7777
}
7878
}
@@ -92,7 +92,7 @@ export const ExecuteVisibleAndCustomTests = async (
9292
}
9393
);
9494

95-
if (response.status === 200) {
95+
if (response.ok) {
9696
return response.json();
9797
} else {
9898
throw new Error(
@@ -116,7 +116,7 @@ export const ExecuteVisibleAndHiddenTestsAndSubmit = async (
116116
}
117117
);
118118

119-
if (response.status === 200) {
119+
if (response.ok) {
120120
return response.json();
121121
} else {
122122
throw new Error(

apps/frontend/src/app/services/question.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,13 @@ export const GetSingleQuestion = async (docRef: string): Promise<Question> => {
9898
}
9999
}
100100
);
101-
const data = await response.json();
102-
return data;
101+
if (response.ok) {
102+
return response.json();
103+
} else {
104+
throw new Error(
105+
`Error reading question: ${await response.text()}`
106+
);
107+
}
103108
};
104109

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

118-
if (response.status === 200) {
123+
if (response.ok) {
119124
return response.json();
120125
} else {
121126
throw new Error(
@@ -140,7 +145,7 @@ export const EditQuestion = async (
140145
}
141146
);
142147

143-
if (response.status === 200) {
148+
if (response.ok) {
144149
return response.json();
145150
} else {
146151
throw new Error(

apps/history-service/models/pagination.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ func PaginateResponse(limit, offset int, histories []SubmissionHistory) *Histori
1414
end := offset + limit
1515

1616
var paginatedHistory []SubmissionHistory
17-
if start < len(histories) {
18-
if end > len(histories) {
17+
if start <= len(histories) {
18+
if end >= len(histories) {
1919
end = len(histories)
2020
}
2121
} else {

0 commit comments

Comments
 (0)