Skip to content

Commit

Permalink
Log github fetch errors to main logger
Browse files Browse the repository at this point in the history
  • Loading branch information
sirbrillig committed Sep 8, 2024
1 parent 1c97924 commit c968b16
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 36 deletions.
30 changes: 2 additions & 28 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,18 @@ import {
import { menubar } from 'menubar';
import isDev from 'electron-is-dev';
import electronDebug from 'electron-debug';
import {
setToken,
getToken,
isLoggingEnabled,
toggleLogging,
} from './lib/main-store';
import { setToken, getToken, toggleLogging } from './lib/main-store';
import { getIconForState } from './lib/icon-path';
import { version } from '../../package.json';
import unhandled from 'electron-unhandled';
import debugFactory from 'debug';
import log from 'electron-log';
import AutoLaunch from 'easy-auto-launch';
import dotEnv from 'dotenv';
import {
fetchNotificationsForAccount,
markNotficationAsRead,
} from './lib/github-interface';
import { logMessage } from './lib/logging';
import type { AccountInfo, Note } from '../shared-types';

// These are provided by electron forge
Expand All @@ -46,27 +41,6 @@ electronDebug();

let lastIconState = 'loading';

// Only use this function for logging!
function logMessage(message: string, level: 'info' | 'warn' | 'error'): void {
debug(message);
if (!isLoggingEnabled()) {
return;
}
switch (level) {
case 'info':
log.info(message);
break;
case 'warn':
log.warn(message);
break;
case 'error':
log.error(message);
break;
default:
log.error(`Unknown log level '${level}': ${message}`);
}
}

const bar = menubar({
preloadWindow: true,
index: MAIN_WINDOW_WEBPACK_ENTRY,
Expand Down
20 changes: 12 additions & 8 deletions src/main/lib/github-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { AccountInfo, Note, NoteReason } from '../../shared-types';
import { Octokit } from '@octokit/rest';
import { fetch as undiciFetch, ProxyAgent } from 'undici';
import { socksDispatcher } from 'fetch-socks';
import { logMessage } from './logging';

const userAgent = 'gitnews-menubar';
const mainGithubApiUrl = 'https://api.github.com';
Expand Down Expand Up @@ -92,8 +93,10 @@ export async function markNotficationAsRead(
thread_id: note.id,
});
} catch (error) {
// FIXME: log these errors in the main logger
console.error(`Failed to mark notification read for ${path}`, note);
logMessage(
`Failed to mark notification read for ${path} (${note.url})`,
'error'
);
return;
}
}
Expand Down Expand Up @@ -121,8 +124,10 @@ export async function fetchNotificationsForAccount(
commentAvatar = comment.data.user.avatar_url;
commentHtmlUrl = comment.data.html_url;
} catch (error) {
// FIXME: log these errors in the main logger
console.error(`Failed to fetch comment for ${commentPath}`, notification);
logMessage(
`Failed to fetch comment for ${commentPath} (${notification.subject.latest_comment_url ?? notification.subject.url})`,
'error'
);
continue;
}

Expand All @@ -139,10 +144,9 @@ export async function fetchNotificationsForAccount(
noteMerged = subject.data.merged;
subjectHtmlUrl = subject.data.html_url;
} catch (error) {
// FIXME: log these errors in the main logger
console.error(
`Failed to fetch subject for ${subjectPath}`,
notification.subject
logMessage(
`Failed to fetch comment for ${subjectPath} (${notification.subject.url})`,
'error'
);
continue;
}
Expand Down
29 changes: 29 additions & 0 deletions src/main/lib/logging.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import log from 'electron-log';
import debugFactory from 'debug';
import { isLoggingEnabled } from './main-store';

const debug = debugFactory('gitnews-menubar');

// Only use this function for logging!
export function logMessage(
message: string,
level: 'info' | 'warn' | 'error'
): void {
debug(message);
if (!isLoggingEnabled()) {
return;
}
switch (level) {
case 'info':
log.info(message);
break;
case 'warn':
log.warn(message);
break;
case 'error':
log.error(message);
break;
default:
log.error(`Unknown log level '${level}': ${message}`);
}
}

0 comments on commit c968b16

Please sign in to comment.