Skip to content

refactor(logging): improve details in logs #1681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/first-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ function isFirstRun() {
}

fs.writeFileSync(configPath, '');
} catch (error) {
log.error('First run: Unable to write firstRun file', error);
} catch (err) {
log.error('First run: Unable to write firstRun file', err);
}

return true;
Expand Down
12 changes: 9 additions & 3 deletions src/renderer/hooks/useNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ export const useNotifications = (): NotificationsState => {
setNotifications(updatedNotifications);
setTrayIconColor(updatedNotifications);
} catch (err) {
log.error('Error occurred while marking notification as read', err);
log.error(
'[markNotificationsAsRead]: Error occurred while marking notifications as read',
err,
);
}

setStatus('success');
Expand Down Expand Up @@ -155,7 +158,10 @@ export const useNotifications = (): NotificationsState => {
setNotifications(updatedNotifications);
setTrayIconColor(updatedNotifications);
} catch (err) {
log.error('Error occurred while marking notifications as done', err);
log.error(
'[markNotificationsAsDone]: error occurred while marking notifications as done',
err,
);
}

setStatus('success');
Expand All @@ -181,7 +187,7 @@ export const useNotifications = (): NotificationsState => {
}
} catch (err) {
log.error(
'Error occurred while unsubscribing from notification thread',
'[unsubscribeNotification]: error occurred while unsubscribing from notification thread',
err,
);
}
Expand Down
9 changes: 7 additions & 2 deletions src/renderer/utils/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,11 @@ export async function getHtmlUrl(url: Link, token: Token): Promise<string> {
const response = (await apiRequestAuth(url, 'GET', token)).data;
return response.html_url;
} catch (err) {
log.error('Error occurred while fetching notification html url', err);
log.error(
'[getHtmlUrl]: error occurred while fetching html url for',
url,
err,
);
}
}

Expand Down Expand Up @@ -268,7 +272,8 @@ export async function getLatestDiscussion(
);
} catch (err) {
log.error(
'Error occurred while fetching notification latest discussion',
'[getLatestDiscussion]: failed to fetch latest discussion for notification',
`[${notification.subject.type}]: ${notification.subject.title} for repository ${notification.repository.full_name}`,
err,
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/renderer/utils/api/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ export async function apiRequestAuth(

nextUrl = getNextURLFromLinkHeader(response);
}
} catch (error) {
log.error('API request failed:', error);
throw error;
} catch (err) {
log.error('[apiRequestAuth]: API request failed:', err);
throw err;
}

return {
Expand Down
8 changes: 6 additions & 2 deletions src/renderer/utils/auth/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,12 @@ export async function refreshAccount(account: Account): Promise<Account> {
account.version = extractHostVersion(
res.headers['x-github-enterprise-version'],
);
} catch (error) {
log.error('Failed to refresh account', error);
} catch (err) {
log.error(
'[refreshAccount]: failed to refresh account for user',
account.user.login,
err,
);
}

return account;
Expand Down
7 changes: 6 additions & 1 deletion src/renderer/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,14 @@ export async function generateGitHubWebUrl(
}
} catch (err) {
log.error(
'Error occurred while attempting to get a specific notification URL. Will fall back to defaults',
'[generateGitHubWebUrl]: failed to resolve specific notification html url for',
`[${notification.subject.type}]: ${notification.subject.title} for repository ${notification.repository.full_name}`,
err,
);
log.warn(
'Will fall back to opening repository root url for',
notification.repository.full_name,
);
}

url.searchParams.set(
Expand Down
18 changes: 10 additions & 8 deletions src/renderer/utils/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,15 @@ export async function getAllNotifications(
notifications: notifications,
error: null,
};
} catch (error) {
} catch (err) {
log.error(
'Error occurred while fetching account notifications',
error,
'[getAllNotifications]: error occurred while fetching account notifications',
err,
);
return {
account: accountNotifications.account,
notifications: [],
error: determineFailureType(error),
error: determineFailureType(err),
};
}
}),
Expand All @@ -187,11 +187,13 @@ export async function enrichNotifications(

try {
additionalSubjectDetails = await getGitifySubjectDetails(notification);
} catch (error) {
log.warn(
`Error occurred while enriching notification ${notification.subject.title} for repository ${notification.repository.full_name}. Continuing with base notification`,
error,
} catch (err) {
log.error(
'[enrichNotifications] failed to enrich notification details for',
`[${notification.subject.type}]: ${notification.subject.title} for repository ${notification.repository.full_name}`,
err,
);
log.warn('Continuing with base notification details');
}

return {
Expand Down
7 changes: 6 additions & 1 deletion src/renderer/utils/subject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1160,6 +1160,10 @@ describe('renderer/utils/subject.ts', () => {
type: 'Issue',
url: 'https://api.github.com/repos/gitify-app/notifications-test/issues/1' as Link,
});
const mockRepository = {
full_name: 'gitify-app/notifications-test',
} as Repository;
mockNotification.repository = mockRepository;

nock('https://api.github.com')
.get('/repos/gitify-app/notifications-test/issues/1')
Expand All @@ -1168,7 +1172,8 @@ describe('renderer/utils/subject.ts', () => {
await getGitifySubjectDetails(mockNotification);

expect(logErrorSpy).toHaveBeenCalledWith(
'Error occurred while fetching details for Issue notification: This issue will throw an error',
'[getGitifySubjectDetails]: failed to fetch details for notification for',
'[Issue]: This issue will throw an error for repository gitify-app/notifications-test',
mockError,
);
});
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/utils/subject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export async function getGitifySubjectDetails(
}
} catch (err) {
log.error(
`Error occurred while fetching details for ${notification.subject.type} notification: ${notification.subject.title}`,
'[getGitifySubjectDetails]: failed to fetch details for notification for',
`[${notification.subject.type}]: ${notification.subject.title} for repository ${notification.repository.full_name}`,
err,
);
}
Expand Down
Loading