Skip to content

Commit 2815fd1

Browse files
authored
fix: error handling for network connection (#2572)
* fix: error handling for network connection Signed-off-by: Adam Setch <adam.setch@outlook.com> * fix: error handling for network connection Signed-off-by: Adam Setch <adam.setch@outlook.com> * fix: error handling for network connection Signed-off-by: Adam Setch <adam.setch@outlook.com> --------- Signed-off-by: Adam Setch <adam.setch@outlook.com>
1 parent 766ea78 commit 2815fd1

2 files changed

Lines changed: 23 additions & 23 deletions

File tree

src/renderer/utils/api/errors.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('renderer/utils/api/errors.ts', () => {
8383
});
8484

8585
it('network error - no status', () => {
86-
const mockError = new RequestError('Network error', 0, {
86+
const mockError = new RequestError('Network error', 500, {
8787
request: {
8888
method: 'GET',
8989
url: 'https://api.github.com',
@@ -94,8 +94,8 @@ describe('renderer/utils/api/errors.ts', () => {
9494
expect(result).toBe(Errors.NETWORK);
9595
});
9696

97-
it('unknown error - unhandled 403', () => {
98-
const mockError = new RequestError('Forbidden', 403, {
97+
it('unknown error - unhandled 418', () => {
98+
const mockError = new RequestError('Forbidden', 418, {
9999
request: {
100100
method: 'GET',
101101
url: 'https://api.github.com',

src/renderer/utils/api/errors.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,26 @@ export function determineFailureType(
2929
if (err instanceof RequestError) {
3030
const status = err.status;
3131

32-
if (status === 401) {
33-
return Errors.BAD_CREDENTIALS;
34-
}
35-
36-
if (status === 403) {
37-
if (message.includes("Missing the 'notifications' scope")) {
38-
return Errors.MISSING_SCOPES;
39-
}
40-
41-
if (
42-
message.includes('API rate limit exceeded') ||
43-
message.includes('You have exceeded a secondary rate limit')
44-
) {
45-
return Errors.RATE_LIMITED;
46-
}
47-
}
48-
49-
// Network-like errors for RequestError (no status or status 0)
50-
if (status === 0 || status === undefined) {
51-
return Errors.NETWORK;
32+
switch (status) {
33+
case 401:
34+
return Errors.BAD_CREDENTIALS;
35+
case 403:
36+
if (message.includes("Missing the 'notifications' scope")) {
37+
return Errors.MISSING_SCOPES;
38+
}
39+
40+
if (
41+
message.includes('API rate limit exceeded') ||
42+
message.includes('You have exceeded a secondary rate limit')
43+
) {
44+
return Errors.RATE_LIMITED;
45+
}
46+
47+
break;
48+
case 500:
49+
return Errors.NETWORK;
50+
default:
51+
break;
5252
}
5353
}
5454

0 commit comments

Comments
 (0)