Skip to content

Commit 4edc4be

Browse files
authoredAug 12, 2024
feat: improve error logging (#1455)
Signed-off-by: Adam Setch <adam.setch@outlook.com>
1 parent cafd2e6 commit 4edc4be

File tree

8 files changed

+53
-14
lines changed

8 files changed

+53
-14
lines changed
 

‎src/hooks/useNotifications.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { act, renderHook, waitFor } from '@testing-library/react';
22
import axios, { AxiosError } from 'axios';
33
import nock from 'nock';
44

5+
import log from 'electron-log';
56
import {
67
mockAuth,
78
mockGitHubCloudAccount,
@@ -16,10 +17,13 @@ import { Errors } from '../utils/constants';
1617
import { useNotifications } from './useNotifications';
1718

1819
describe('hooks/useNotifications.ts', () => {
20+
const logErrorSpy = jest.spyOn(log, 'error').mockImplementation();
21+
1922
beforeEach(() => {
2023
// axios will default to using the XHR adapter which can't be intercepted
2124
// by nock. So, configure axios to use the node adapter.
2225
axios.defaults.adapter = 'http';
26+
logErrorSpy.mockReset();
2327
});
2428

2529
const id = mockSingleNotification.id;
@@ -294,6 +298,7 @@ describe('hooks/useNotifications.ts', () => {
294298
});
295299

296300
expect(result.current.globalError).toBe(Errors.BAD_CREDENTIALS);
301+
expect(logErrorSpy).toHaveBeenCalledTimes(2);
297302
});
298303

299304
it('should fetch notifications with different failures', async () => {
@@ -336,6 +341,7 @@ describe('hooks/useNotifications.ts', () => {
336341
});
337342

338343
expect(result.current.globalError).toBeNull();
344+
expect(logErrorSpy).toHaveBeenCalledTimes(2);
339345
});
340346
});
341347

@@ -374,6 +380,7 @@ describe('hooks/useNotifications.ts', () => {
374380
});
375381

376382
expect(result.current.notifications.length).toBe(0);
383+
expect(logErrorSpy).toHaveBeenCalledTimes(1);
377384
});
378385
});
379386

@@ -412,6 +419,7 @@ describe('hooks/useNotifications.ts', () => {
412419
});
413420

414421
expect(result.current.notifications.length).toBe(0);
422+
expect(logErrorSpy).toHaveBeenCalledTimes(1);
415423
});
416424
});
417425

@@ -470,6 +478,7 @@ describe('hooks/useNotifications.ts', () => {
470478
});
471479

472480
expect(result.current.notifications.length).toBe(0);
481+
expect(logErrorSpy).toHaveBeenCalledTimes(1);
473482
});
474483
});
475484

@@ -516,6 +525,7 @@ describe('hooks/useNotifications.ts', () => {
516525
});
517526

518527
expect(result.current.notifications.length).toBe(0);
528+
expect(logErrorSpy).toHaveBeenCalledTimes(1);
519529
});
520530
});
521531

‎src/hooks/useNotifications.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import log from 'electron-log';
12
import { useCallback, useState } from 'react';
23
import type {
34
AccountNotifications,
@@ -109,10 +110,11 @@ export const useNotifications = (): NotificationsState => {
109110

110111
setNotifications(updatedNotifications);
111112
setTrayIconColor(updatedNotifications);
112-
setStatus('success');
113113
} catch (err) {
114-
setStatus('success');
114+
log.error('Error occurred while marking notification as read', err);
115115
}
116+
117+
setStatus('success');
116118
},
117119
[notifications],
118120
);
@@ -138,10 +140,11 @@ export const useNotifications = (): NotificationsState => {
138140

139141
setNotifications(updatedNotifications);
140142
setTrayIconColor(updatedNotifications);
141-
setStatus('success');
142143
} catch (err) {
143-
setStatus('success');
144+
log.error('Error occurred while marking notification as done', err);
144145
}
146+
147+
setStatus('success');
145148
},
146149
[notifications],
147150
);
@@ -157,10 +160,14 @@ export const useNotifications = (): NotificationsState => {
157160
notification.account.token,
158161
);
159162
await markNotificationRead(state, notification);
160-
setStatus('success');
161163
} catch (err) {
162-
setStatus('success');
164+
log.error(
165+
'Error occurred while unsubscribing from notification thread',
166+
err,
167+
);
163168
}
169+
170+
setStatus('success');
164171
},
165172
[markNotificationRead],
166173
);
@@ -187,10 +194,14 @@ export const useNotifications = (): NotificationsState => {
187194

188195
setNotifications(updatedNotifications);
189196
setTrayIconColor(updatedNotifications);
190-
setStatus('success');
191197
} catch (err) {
192-
setStatus('success');
198+
log.error(
199+
'Error occurred while marking repository notifications as read',
200+
err,
201+
);
193202
}
203+
204+
setStatus('success');
194205
},
195206
[notifications],
196207
);
@@ -230,10 +241,14 @@ export const useNotifications = (): NotificationsState => {
230241

231242
setNotifications(updatedNotifications);
232243
setTrayIconColor(updatedNotifications);
233-
setStatus('success');
234244
} catch (err) {
235-
setStatus('success');
245+
log.error(
246+
'Error occurred while marking repository notifications as done',
247+
err,
248+
);
236249
}
250+
251+
setStatus('success');
237252
},
238253
[notifications, markNotificationDone],
239254
);

‎src/routes/Login.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const LoginRoute: FC = () => {
2323
try {
2424
await login();
2525
} catch (err) {
26-
// Skip
26+
log.error('Auth: failed to login with GitHub', err);
2727
}
2828
}, []); */
2929

‎src/routes/LoginWithOAuthApp.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { BookIcon, PersonIcon, SignInIcon } from '@primer/octicons-react';
2+
import log from 'electron-log';
23
import { type FC, useCallback, useContext } from 'react';
34
import { Form, type FormRenderProps } from 'react-final-form';
45
import { Header } from '../components/Header';
@@ -120,6 +121,7 @@ export const LoginWithOAuthApp: FC = () => {
120121
try {
121122
await loginWithOAuthApp(data as LoginOAuthAppOptions);
122123
} catch (err) {
124+
log.error('Auth: Failed to login with oauth app', err);
123125
// Skip
124126
}
125127
},

‎src/routes/LoginWithPersonalAccessToken.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { BookIcon, KeyIcon, SignInIcon } from '@primer/octicons-react';
2+
import log from 'electron-log';
23
import { type FC, useCallback, useContext, useState } from 'react';
34
import { Form, type FormRenderProps } from 'react-final-form';
45
import { useNavigate } from 'react-router-dom';
@@ -125,6 +126,7 @@ export const LoginWithPersonalAccessToken: FC = () => {
125126
);
126127
navigate(-1);
127128
} catch (err) {
129+
log.error('Auth: failed to login with personal access token', err);
128130
setIsValidToken(false);
129131
}
130132
},

‎src/utils/api/client.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ describe('utils/api/client.ts', () => {
292292
'123' as Token,
293293
);
294294

295-
expect(logErrorSpy).toHaveBeenCalledWith('Failed to get html url');
295+
expect(logErrorSpy).toHaveBeenCalledTimes(1);
296296
});
297297
});
298298
});

‎src/utils/api/client.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ export async function getHtmlUrl(url: Link, token: Token): Promise<string> {
226226
const response = (await apiRequestAuth(url, 'GET', token)).data;
227227
return response.html_url;
228228
} catch (err) {
229-
log.error('Failed to get html url');
229+
log.error('Error occurred while fetching notification html url', err);
230230
}
231231
}
232232

@@ -272,5 +272,10 @@ export async function getLatestDiscussion(
272272
(discussion) => discussion.title === notification.subject.title,
273273
)[0] ?? null
274274
);
275-
} catch (err) {}
275+
} catch (err) {
276+
log.error(
277+
'Error occurred while fetching notification latest discussion',
278+
err,
279+
);
280+
}
276281
}

‎src/utils/notifications.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import log from 'electron-log';
12
import type {
23
AccountNotifications,
34
GitifyState,
@@ -148,6 +149,10 @@ export async function getAllNotifications(
148149
error: null,
149150
};
150151
} catch (error) {
152+
log.error(
153+
'Error occurred while fetching account notifications',
154+
error,
155+
);
151156
return {
152157
account: accountNotifications.account,
153158
notifications: [],

0 commit comments

Comments
 (0)