Skip to content

Commit

Permalink
fix: sentry imports
Browse files Browse the repository at this point in the history
  • Loading branch information
mtdvlpr committed Dec 6, 2024
1 parent 84ec937 commit 01b8cf2
Show file tree
Hide file tree
Showing 21 changed files with 107 additions and 85 deletions.
2 changes: 1 addition & 1 deletion src-electron/electron-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { join } from 'upath';

import { PLATFORM } from './constants';
import { cancelAllDownloads } from './main/downloads';
import { captureElectronError } from './main/log';
import { initScreenListeners } from './main/screen';
import { initSessionListeners } from './main/session';
import { initUpdater } from './main/updater';
Expand All @@ -25,7 +26,6 @@ import {
createMainWindow,
mainWindow,
} from './main/window/window-main';
import { captureElectronError } from './utils';

if (process.env.PORTABLE_EXECUTABLE_DIR) {
app.setPath('appData', process.env.PORTABLE_EXECUTABLE_DIR);
Expand Down
3 changes: 2 additions & 1 deletion src-electron/main/downloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { ElectronDownloadManager } from 'electron-dl-manager';
import { ensureDir } from 'fs-extra';
import { basename } from 'upath';

import { captureElectronError, fetchJson } from './../utils';
import { fetchJson } from './../utils';
import { captureElectronError } from './log';
import { sendToWindow } from './window/window-base';
import { mainWindow } from './window/window-main';

Expand Down
2 changes: 1 addition & 1 deletion src-electron/main/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from 'src/constants/media';
import { basename, dirname, join, toUnix } from 'upath';

import { captureElectronError } from '../utils';
import { captureElectronError } from './log';
import { sendToWindow } from './window/window-base';
import { mainWindow } from './window/window-main';

Expand Down
3 changes: 2 additions & 1 deletion src-electron/main/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from 'electron';

import { PLATFORM } from '../constants';
import { askForMediaAccess, getAppVersion, isSelf } from './../utils';
import { getAppVersion, isSelf } from './../utils';
import { downloadFile } from './downloads';
import {
openFileDialog,
Expand All @@ -40,6 +40,7 @@ import { logToWindow } from './window/window-base';
import { mainWindow, toggleAuthorizedClose } from './window/window-main';
import { mediaWindow, moveMediaWindow } from './window/window-media';
import {
askForMediaAccess,
createWebsiteWindow,
navigateWebsiteWindow,
websiteWindow,
Expand Down
26 changes: 26 additions & 0 deletions src-electron/main/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { ExclusiveEventHintOrCaptureContext } from '@sentry/core/build/types/utils/prepareEvent';

import { captureException } from '@sentry/electron/main';

import { IS_DEV } from '../constants';

/**
* Logs an error to the console or to Sentry
* @param error The error to log
* @param context The context to log with the error
*/
export function captureElectronError(
error: Error | string | unknown,
context?: ExclusiveEventHintOrCaptureContext,
) {
if (error instanceof Error && error.cause) {
captureElectronError(error.cause, context);
}

if (IS_DEV) {
console.error(error);
console.warn('context', context);
} else {
captureException(error, context);
}
}
2 changes: 1 addition & 1 deletion src-electron/main/screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Display, ScreenPreferences } from 'src/types';

import { app, type BrowserWindow, screen } from 'electron';

import { captureElectronError } from '../utils';
import { captureElectronError } from './log';
import { mainWindow } from './window/window-main';
import { mediaWindow, moveMediaWindow } from './window/window-media';

Expand Down
2 changes: 1 addition & 1 deletion src-electron/main/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { SettingsValues } from 'src/types';

import { globalShortcut } from 'electron';

import { captureElectronError } from '../utils';
import { captureElectronError } from './log';
import { sendToWindow } from './window/window-base';
import { mainWindow } from './window/window-main';

Expand Down
2 changes: 1 addition & 1 deletion src-electron/main/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { exists } from 'fs-extra';
import { join } from 'path';

import { PLATFORM } from './../constants';
import { captureElectronError } from './../utils';
import { captureElectronError } from './log';

export async function initUpdater() {
autoUpdater.on('error', (error, message) => {
Expand Down
4 changes: 2 additions & 2 deletions src-electron/main/window/window-base.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { ElectronIpcListenKey } from 'src/types';

import { IS_DEV, PLATFORM } from 'app/src-electron/constants';
import { captureElectronError } from 'app/src-electron/utils';
import {
app,
BrowserWindow,
type BrowserWindowConstructorOptions,
} from 'electron';
import { join, resolve } from 'path';

import { urlVariables } from '../session';
import { captureElectronError } from './../log';
import { urlVariables } from './../session';
import { StatefulBrowserWindow } from './window-state';

export function closeOtherWindows(source: BrowserWindow) {
Expand Down
4 changes: 2 additions & 2 deletions src-electron/main/window/window-media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import type { BrowserWindow } from 'electron';
import type { ScreenPreferences } from 'src/types';

import { HD_RESOLUTION, PLATFORM } from 'app/src-electron/constants';
import { captureElectronError } from 'app/src-electron/utils';
import { join, resolve } from 'path';

import { getAllScreens, getWindowScreen, screenPreferences } from '../screen';
import { captureElectronError } from './../log';
import { getAllScreens, getWindowScreen, screenPreferences } from './../screen';
import { createWindow, sendToWindow } from './window-base';
import { mainWindow } from './window-main';

Expand Down
3 changes: 2 additions & 1 deletion src-electron/main/window/window-state.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { captureElectronError } from 'app/src-electron/utils';
import {
app,
BrowserWindow,
Expand All @@ -9,6 +8,8 @@ import {
import { ensureDirSync, readJsonSync, writeJsonSync } from 'fs-extra';
import { dirname, join } from 'path';

import { captureElectronError } from './../log';

interface ExtraOptions {
/** The name of file. Defaults to `window-state.json`. */
configFileName?: string;
Expand Down
30 changes: 24 additions & 6 deletions src-electron/main/window/window-website.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import type { BrowserWindow, Video } from 'electron';
import type { NavigateWebsiteAction } from 'src/types';

import { HD_RESOLUTION, PLATFORM } from 'app/src-electron/constants';
import {
askForMediaAccess,
captureElectronError,
} from 'app/src-electron/utils';
import { type BrowserWindow, systemPreferences, type Video } from 'electron';

import { createWindow, sendToWindow } from './window-base';
import { captureElectronError } from './../log';
import { createWindow, logToWindow, sendToWindow } from './window-base';
import { mainWindow } from './window-main';

export let websiteWindow: BrowserWindow | null = null;
Expand Down Expand Up @@ -141,6 +138,27 @@ export async function createWebsiteWindow(lang?: string) {
});
}

/**
* Asks for media access for the camera and microphone
*/
export const askForMediaAccess = async () => {
if (PLATFORM !== 'darwin') return;
const types = ['camera', 'microphone'] as const;

for (const type of types) {
try {
const access = systemPreferences.getMediaAccessStatus(type);
if (access !== 'granted') {
logToWindow(mainWindow, `No ${type} access`, access, 'error');
const result = await systemPreferences.askForMediaAccess(type);
logToWindow(mainWindow, `${type} result:`, result, 'debug');
}
} catch (e) {
captureElectronError(e);
}
}
};

export const zoomWebsiteWindow = (direction: 'in' | 'out') => {
if (!websiteWindow) return;
try {
Expand Down
8 changes: 4 additions & 4 deletions src-electron/preload/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ensureDir, writeFile } from 'fs-extra';
import { FULL_HD } from 'src/constants/media';
import { basename, join } from 'upath';

import { captureElectronError } from '../utils';
import { capturePreloadError } from './log';

export interface ConversionOptions {
/**
Expand Down Expand Up @@ -42,7 +42,7 @@ export const getNrOfPdfPages = async (pdfPath: string): Promise<number> => {
const pdfDocument = await loadingTask.promise;
return pdfDocument.numPages;
} catch (e) {
captureElectronError(e);
capturePreloadError(e);
return 0;
}
};
Expand Down Expand Up @@ -98,12 +98,12 @@ export const convertPdfToImages = async (
await writeFile(outputPath, base64Data, 'base64');
outputImages.push(outputPath);
} catch (e) {
captureElectronError(e);
capturePreloadError(e);
}
}
return outputImages;
} catch (e) {
captureElectronError(e);
capturePreloadError(e);
return outputImages;
}
};
4 changes: 2 additions & 2 deletions src-electron/preload/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { VideoDuration } from 'src/types';

import url from 'url';

import { captureElectronError } from '../utils';
import { capturePreloadError } from './log';

export const getVideoDuration = async (
filePath: string,
Expand All @@ -22,7 +22,7 @@ const isFileUrl = (path: string) => {
try {
return path.startsWith('file://');
} catch (err) {
captureElectronError(err, {
capturePreloadError(err, {
contexts: { fn: { name: 'isFileUrl', path } },
});
return false;
Expand Down
26 changes: 26 additions & 0 deletions src-electron/preload/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { ExclusiveEventHintOrCaptureContext } from '@sentry/core/build/types/utils/prepareEvent';

import { captureException } from '@sentry/electron/renderer';

import { IS_DEV } from '../constants';

/**
* Logs an error to the console or to Sentry
* @param error The error to log
* @param context The context to log with the error
*/
export function capturePreloadError(
error: Error | string | unknown,
context?: ExclusiveEventHintOrCaptureContext,
) {
if (error instanceof Error && error.cause) {
capturePreloadError(error.cause, context);
}

if (IS_DEV) {
console.error(error);
console.warn('context', context);
} else {
captureException(error, context);
}
}
4 changes: 2 additions & 2 deletions src-electron/preload/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { QueryResponseItem } from 'src/types';

import BetterSqlite3 from 'better-sqlite3';

import { captureElectronError } from '../utils';
import { capturePreloadError } from './log';

export const executeQuery = <T = QueryResponseItem>(
dbPath: string,
Expand All @@ -21,7 +21,7 @@ export const executeQuery = <T = QueryResponseItem>(
});
return result;
} catch (e) {
captureElectronError(e, {
capturePreloadError(e, {
contexts: { fn: { name: 'executeQuery', path: dbPath, query } },
});
return [];
Expand Down
52 changes: 3 additions & 49 deletions src-electron/utils.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,9 @@
import type { ExclusiveEventHintOrCaptureContext } from '@sentry/core/build/types/utils/prepareEvent';

import { captureException } from '@sentry/electron/main';
import { version } from 'app/package.json';
import { app, systemPreferences } from 'electron';
import { app } from 'electron';

import { IS_DEV, JW_DOMAINS, PLATFORM, TRUSTED_DOMAINS } from './constants';
import { IS_DEV, JW_DOMAINS, TRUSTED_DOMAINS } from './constants';
import { captureElectronError } from './main/log';
import { urlVariables } from './main/session';
import { logToWindow } from './main/window/window-base';
import { mainWindow } from './main/window/window-main';

/**
* Asks for media access for the camera and microphone
*/
export async function askForMediaAccess() {
if (PLATFORM !== 'darwin') return;
const types = ['camera', 'microphone'] as const;

for (const type of types) {
try {
const access = systemPreferences.getMediaAccessStatus(type);
if (access !== 'granted') {
logToWindow(mainWindow, `No ${type} access`, access, 'error');
const result = await systemPreferences.askForMediaAccess(type);
logToWindow(mainWindow, `${type} result:`, result, 'debug');
}
} catch (e) {
captureElectronError(e);
}
}
}

/**
* Gets the current app version
Expand Down Expand Up @@ -179,27 +154,6 @@ export const fetchJson = async <T>(
return null;
};

/**
* Logs an error to the console or to Sentry
* @param error The error to log
* @param context The context to log with the error
*/
export function captureElectronError(
error: Error | string | unknown,
context?: ExclusiveEventHintOrCaptureContext,
) {
if (error instanceof Error && error.cause) {
captureElectronError(error.cause, context);
}

if (IS_DEV) {
console.error(error);
console.warn('context', context);
} else {
captureException(error, context);
}
}

/**
* Throttles a function to only run once every `delay` milliseconds
* @param func The function to throttle
Expand Down
4 changes: 1 addition & 3 deletions src/components/dialog/DialogCacheClear.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ const props = defineProps<{
unusedParentDirectories: Record<string, number>;
}>();
const { fs } = window.electronApi;
const jwStore = useJwStore();
const { additionalMediaMaps, lookupPeriod } = storeToRefs(jwStore);
Expand Down Expand Up @@ -76,7 +74,7 @@ const deleteCacheFiles = async (type = '') => {
: props.cacheFiles.map((f) => f.path);
for (const filepath of filepathsToDelete) {
try {
fs.remove(filepath);
window.electronApi.fs.remove(filepath);
if (
filepath.startsWith(await getAdditionalMediaPath()) ||
filepath.startsWith(
Expand Down
5 changes: 2 additions & 3 deletions src/components/media/DragAndDropper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ import { errorCatcher } from 'src/helpers/error-catcher';
import { addJwpubDocumentMediaToFiles } from 'src/helpers/jw-media';
import { computed, ref, useTemplateRef } from 'vue';
const { openFileDialog } = window.electronApi;
const props = defineProps<{
currentFile: number;
section: MediaSection | undefined;
Expand Down Expand Up @@ -163,7 +161,8 @@ const resetModal = () => {
};
const getLocalFiles = async () => {
openFileDialog()
window.electronApi
.openFileDialog()
.then((result) => {
if (result && result.filePaths.length > 0) {
window.dispatchEvent(
Expand Down
Loading

0 comments on commit 01b8cf2

Please sign in to comment.