Skip to content

Commit

Permalink
fix: Catch download errors (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyaiox authored Oct 7, 2024
1 parent f9491fa commit 7b98a87
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/main/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getOSName, PLATFORM } from './helpers';
// Initialize Sentry
Sentry.init({
dsn: 'https://47fa1e9f4bfed88d2a4b2bdd7b3d48b6@o4504361728212992.ingest.us.sentry.io/4507924285161472',
enabled: import.meta.env.PROD,
});

// Initialize logger
Expand Down
20 changes: 19 additions & 1 deletion packages/main/src/mainWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { initIpcHandlers } from './modules/ipc';
import { getAppIcon, getAdditionalArguments } from './helpers';

async function createWindow() {
await initIpcHandlers();
initIpcHandlers();

const browserWindow = new BrowserWindow({
show: false, // Use the 'ready-to-show' event to show the instantiated BrowserWindow.
Expand Down Expand Up @@ -84,4 +84,22 @@ export async function restoreOrCreateWindow() {
}

window.focus();

// Listen to active downloads and cancel them when the window is closed.
let activeDownloads: Electron.DownloadItem[] = [];
window.webContents.session.on('will-download', (_, item) => {
activeDownloads.push(item);
item.on('done', () => {
activeDownloads = activeDownloads.filter(_item => _item !== item);
});
});

window.on('close', _ => {
// Cancel all active downloads
activeDownloads.forEach(item => {
if (item.getState() === 'progressing') {
item.cancel();
}
});
});
}
10 changes: 7 additions & 3 deletions packages/main/src/modules/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { join } from 'path';
import fs from 'node:fs';
import { spawn } from 'child_process';
import { app, BrowserWindow, ipcMain } from 'electron';
import { download } from 'electron-dl';
import { CancelError, download } from 'electron-dl';
import log from 'electron-log/main';
import { Analytics, IPC_EVENTS, IPC_EVENT_DATA_TYPE, ANALYTICS_EVENT, IPC_HANDLERS, getErrorMessage } from '#shared';
import { getAppBasePath, decompressFile, getOSName, isAppUpdated, PLATFORM, getAppVersion } from '../helpers';
Expand Down Expand Up @@ -102,8 +102,12 @@ export async function downloadExplorer(event: Electron.IpcMainInvokeEvent, url:
});
}
} catch (error) {
log.error('[Main Window][IPC][DownloadExplorer] Error Downloading', url, error);
event.sender.send(IPC_EVENTS.DOWNLOAD_STATE, { type: IPC_EVENT_DATA_TYPE.ERROR, error });
if (error instanceof CancelError) {
log.error('[Main Window][IPC][DownloadExplorer] Download Cancelled');
} else {
log.error('[Main Window][IPC][DownloadExplorer] Error Downloading', url, error);
event.sender.send(IPC_EVENTS.DOWNLOAD_STATE, { type: IPC_EVENT_DATA_TYPE.ERROR, error });
}
}

return null;
Expand Down
5 changes: 5 additions & 0 deletions packages/main/tests/unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ vi.mock('electron', () => {
bw.prototype.focus = vi.fn();
bw.prototype.restore = vi.fn();
bw.prototype.setMenuBarVisibility = vi.fn();
bw.prototype.webContents = {
session: {
on: vi.fn(),
},
};

const app: Pick<Electron.App, 'getAppPath' | 'getPath' | 'getVersion' | 'on'> = {
getAppPath(): string {
Expand Down

0 comments on commit 7b98a87

Please sign in to comment.