Skip to content

Commit 40952db

Browse files
committed
Modify error handling
1 parent 3b0f530 commit 40952db

File tree

2 files changed

+25
-40
lines changed

2 files changed

+25
-40
lines changed

src/commons/api/index.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,19 @@ export const customFetch = async (
3434
const shouldRefresh = opts.shouldRefresh ?? true;
3535
const shouldAutoLogout = opts.shouldAutoLogout ?? true;
3636

37+
const resp = await fetch(url, opts as RequestInit);
3738
try {
38-
const resp = await fetch(url, opts as RequestInit);
39-
4039
// response.ok is (200 <= response.status <= 299)
4140
// response.status of > 299 does not raise error; so deal with in in the try clause
4241

4342
// Refresh the user's tokens if a response status of 401 was obtained.
44-
if (shouldRefresh && resp && resp.status === 401) {
43+
if (shouldRefresh && resp.status === 401) {
4544
const newTokens = await Cadet.auth.refresh({ refresh_token: opts.refreshToken! });
4645
const tokens = newTokens.data!;
4746
store.dispatch(
4847
actions.setTokens({
49-
accessToken: tokens.access_token!,
50-
refreshToken: tokens.refresh_token!
48+
accessToken: tokens.access_token,
49+
refreshToken: tokens.refresh_token
5150
})
5251
);
5352
const newOpts = {
@@ -58,14 +57,14 @@ export const customFetch = async (
5857
return customFetch(url, newOpts);
5958
}
6059

61-
if (resp && !resp.ok && !shouldAutoLogout) {
60+
if (!resp.ok && !shouldAutoLogout) {
6261
// this clause is mostly for SUBMIT_ANSWER; show an error message instead
6362
// and ask student to manually logout, so that they have a chance to save
6463
// their answers
6564
return resp;
6665
}
6766

68-
if (!resp || !resp.ok) {
67+
if (!resp.ok) {
6968
throw new Error('API call failed or got non-OK response');
7069
}
7170

@@ -75,7 +74,7 @@ export const customFetch = async (
7574
store.dispatch(actions.logOut());
7675
showWarningMessage(opts.errorMessage ? opts.errorMessage : 'Please login again.');
7776

78-
return null;
77+
return resp;
7978
}
8079
};
8180

src/commons/sagas/RequestsSaga.ts

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export const postAuth = async (
8585
shouldRefresh: false
8686
}
8787
);
88-
if (!resp) {
88+
if (!resp.ok) {
8989
return null;
9090
}
9191

@@ -101,7 +101,7 @@ export const postAuth = async (
101101
*/
102102
const postRefresh = async (refreshToken: string): Promise<Tokens | null> => {
103103
const resp = await Cadet.auth.refresh({ refresh_token: refreshToken });
104-
if (!resp) {
104+
if (!resp.ok) {
105105
return null;
106106
}
107107

@@ -118,7 +118,7 @@ const postRefresh = async (refreshToken: string): Promise<Tokens | null> => {
118118
export const getUser = async (tokens: Tokens): Promise<User | null> => {
119119
const resp = await Cadet.user.index({ ...tokens });
120120
// TODO
121-
return resp && resp.ok ? (resp.data as User) : null;
121+
return resp.ok ? (resp.data as User) : null;
122122
};
123123

124124
/**
@@ -129,7 +129,7 @@ export const getUser = async (tokens: Tokens): Promise<User | null> => {
129129
export const getAchievements = async (tokens: Tokens): Promise<AchievementItem[] | null> => {
130130
const resp = await Cadet.incentives.indexAchievements({ ...tokens });
131131

132-
if (!resp || !resp.ok) {
132+
if (!resp.ok) {
133133
return null; // invalid accessToken _and_ refreshToken
134134
}
135135

@@ -195,7 +195,7 @@ export const getGoals = async (
195195
export const getOwnGoals = async (tokens: Tokens): Promise<AchievementGoal[] | null> => {
196196
const resp = await Cadet.incentives.indexGoals({ ...tokens });
197197

198-
if (!resp || !resp.ok) {
198+
if (!resp.ok) {
199199
return null; // invalid accessToken _and_ refreshToken
200200
}
201201

@@ -316,7 +316,7 @@ export const getAssessmentOverviews = async (
316316
tokens: Tokens
317317
): Promise<AssessmentOverview[] | null> => {
318318
const resp = await Cadet.assessments.index({ ...tokens });
319-
if (!resp || !resp.ok) {
319+
if (!resp.ok) {
320320
return null; // invalid accessToken _and_ refreshToken
321321
}
322322
const assessmentOverviews = resp.data;
@@ -369,7 +369,7 @@ export const getAssessment = async (id: number, tokens: Tokens): Promise<Assessm
369369
resp = await Cadet.assessments.unlock(id, { password: input }, { ...tokens });
370370
}
371371

372-
if (!resp || !resp.ok) {
372+
if (!resp.ok) {
373373
return null;
374374
}
375375

@@ -420,17 +420,15 @@ export const postAnswer = async (
420420
answer: string | number,
421421
tokens: Tokens
422422
): Promise<Response | null> => {
423-
const resp = await Cadet.answer.submit(id, { answer }, tokens);
423+
const resp = await Cadet.answer.submit(id, { answer }, { ...tokens });
424424
return resp;
425425
};
426426

427427
/**
428428
* POST /assessments/{assessmentId}/submit
429429
*/
430430
export const postAssessment = async (id: number, tokens: Tokens): Promise<Response | null> => {
431-
const resp = await Cadet.assessments.submit(id, tokens);
432-
// shouldAutoLogout: false, // 400 if some questions unattempted
433-
431+
const resp = await Cadet.assessments.submit(id, { ...tokens });
434432
return resp;
435433
};
436434

@@ -442,7 +440,7 @@ export const getGradingOverviews = async (
442440
group: boolean
443441
): Promise<GradingOverview[] | null> => {
444442
const resp = await Cadet.adminGrading.index({ group }, { ...tokens });
445-
if (!resp) {
443+
if (!resp.ok) {
446444
return null; // invalid accessToken _and_ refreshToken
447445
}
448446
const gradingOverviews = resp.data;
@@ -495,7 +493,7 @@ export const getGradingOverviews = async (
495493
export const getGrading = async (submissionId: number, tokens: Tokens): Promise<Grading | null> => {
496494
const resp = await Cadet.adminGrading.show(submissionId, { ...tokens });
497495

498-
if (!resp) {
496+
if (!resp.ok) {
499497
return null;
500498
}
501499

@@ -608,7 +606,7 @@ export const getNotifications = async (tokens: Tokens): Promise<Notification[]>
608606

609607
let notifications: Notification[] = [];
610608

611-
if (!resp || !resp.ok) {
609+
if (!resp.ok) {
612610
return notifications;
613611
}
614612

@@ -651,7 +649,7 @@ export const postAcknowledgeNotifications = async (
651649
export const getSourcecastIndex = async (tokens: Tokens): Promise<SourcecastData[] | null> => {
652650
const resp = await Cadet.sourcecast.index({ ...tokens });
653651
// TODO
654-
return resp && resp.ok ? ((resp.data as unknown) as SourcecastData[]) : null;
652+
return resp.ok ? ((resp.data as unknown) as SourcecastData[]) : null;
655653
};
656654

657655
/**
@@ -733,15 +731,15 @@ export const uploadAssessment = async (
733731
*/
734732
export const getGradingSummary = async (tokens: Tokens): Promise<GradingSummary | null> => {
735733
const resp = await Cadet.adminGrading.gradingSummary({ ...tokens });
736-
return resp && resp.ok ? resp.data : null;
734+
return resp.ok ? resp.data : null;
737735
};
738736

739737
/**
740738
* GET /settings/sublanguage
741739
*/
742740
export const getSublanguage = async (): Promise<SourceLanguage | null> => {
743741
const resp = await Cadet.settings.index();
744-
if (!resp || !resp.ok) {
742+
if (!resp.ok) {
745743
return null;
746744
}
747745

@@ -774,7 +772,7 @@ export const postSublanguage = async (
774772
*/
775773
export async function fetchDevices(tokens: Tokens): Promise<Device[] | null> {
776774
const resp = await Cadet.devices.index({ ...tokens });
777-
return resp && resp.ok ? resp.data : null;
775+
return resp.ok ? resp.data : null;
778776
}
779777

780778
/**
@@ -785,7 +783,7 @@ export async function getDeviceWSEndpoint(
785783
tokens: Tokens
786784
): Promise<WebSocketEndpointInformation | null> {
787785
const resp = await Cadet.devices.getWsEndpoint(device.id, { ...tokens });
788-
return resp && resp.ok ? resp.data : null;
786+
return resp.ok ? resp.data : null;
789787
}
790788

791789
/**
@@ -795,12 +793,8 @@ export async function registerDevice(device: Omit<Device, 'id'>, tokens?: Tokens
795793
tokens = fillTokens(tokens);
796794
const resp = await Cadet.devices.register(device, { ...tokens });
797795

798-
if (!resp) {
799-
throw new Error('Unknown error occurred.');
800-
}
801-
802796
if (!resp.ok) {
803-
const message = resp.text();
797+
const message = await resp.text();
804798
throw new Error(`Failed to register: ${message}`);
805799
}
806800

@@ -817,10 +811,6 @@ export async function editDevice(
817811
tokens = fillTokens(tokens);
818812
const resp = await Cadet.devices.edit(device.id, device, { ...tokens });
819813

820-
if (!resp) {
821-
throw new Error('Unknown error occurred.');
822-
}
823-
824814
if (!resp.ok) {
825815
const message = await resp.text();
826816
throw new Error(`Failed to edit: ${message}`);
@@ -836,10 +826,6 @@ export async function deleteDevice(device: Pick<Device, 'id'>, tokens?: Tokens):
836826
tokens = fillTokens(tokens);
837827
const resp = await Cadet.devices.deregister(device.id, { ...tokens });
838828

839-
if (!resp) {
840-
throw new Error('Unknown error occurred.');
841-
}
842-
843829
if (!resp.ok) {
844830
const message = await resp.text();
845831
throw new Error(`Failed to delete: ${message}`);

0 commit comments

Comments
 (0)