From 17a87e7cb4cb11a545fd0ebb70937c41850432f9 Mon Sep 17 00:00:00 2001 From: lin onetwo Date: Wed, 21 Jan 2026 15:43:55 +0800 Subject: [PATCH] Improve error handling for window and view initialization Enhanced error reporting and handling when browser windows are not ready or fail to register in windowService. Updated focus logic to dynamically retrieve the current browser window, improving reliability during workspace hibernation and wake-up scenarios. --- src/services/view/index.ts | 8 ++++++-- src/services/view/setupViewEventHandlers.ts | 4 +++- src/services/windows/handleCreateBasicWindow.ts | 4 ++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/services/view/index.ts b/src/services/view/index.ts index ff25eed0..4cffea92 100644 --- a/src/services/view/index.ts +++ b/src/services/view/index.ts @@ -256,8 +256,12 @@ export class View implements IViewService { return; } if (browserWindow === undefined) { - logger.warn(`BrowserViewService.addView: ${workspace.id} 's browser window is not ready`); - return; + logger.error(`BrowserViewService.addView: ${workspace.id} 's browser window is not ready`, { + windowName, + workspaceId: workspace.id, + workspaceName: workspace.name, + }); + throw new Error(`Browser window ${windowName} is not ready for workspace ${workspace.id}`); } const sharedWebPreferences = await this.getSharedWebPreferences(workspace); const view = await this.createViewAddToWindow(workspace, browserWindow, sharedWebPreferences, windowName); diff --git a/src/services/view/setupViewEventHandlers.ts b/src/services/view/setupViewEventHandlers.ts index a871eb10..fe0e6ce9 100644 --- a/src/services/view/setupViewEventHandlers.ts +++ b/src/services/view/setupViewEventHandlers.ts @@ -125,7 +125,9 @@ export default function setupViewEventHandlers( }); // focus on initial load // https://github.com/atomery/webcatalog/issues/398 - if (workspace.active && !browserWindow.isDestroyed() && browserWindow.isFocused() && !view.webContents.isFocused()) { + // Get current browser window dynamically to handle workspace hibernation/wake-up scenarios + const currentBrowserWindow = BrowserWindow.fromWebContents(view.webContents); + if (currentBrowserWindow && workspace.active && !currentBrowserWindow.isDestroyed() && currentBrowserWindow.isFocused() && !view.webContents.isFocused()) { view.webContents.focus(); } // update isLoading to false when load succeed diff --git a/src/services/windows/handleCreateBasicWindow.ts b/src/services/windows/handleCreateBasicWindow.ts index b9dedf41..8482cfe2 100644 --- a/src/services/windows/handleCreateBasicWindow.ts +++ b/src/services/windows/handleCreateBasicWindow.ts @@ -22,6 +22,10 @@ export async function handleCreateBasicWindow( const newWindowURL = (windowMeta !== undefined && 'uri' in windowMeta ? windowMeta.uri : undefined) ?? getMainWindowEntry(); if (config?.multiple !== true) { windowService.set(windowName, newWindow); + const verifySet = windowService.get(windowName); + if (verifySet === undefined) { + throw new Error(`Failed to set window ${windowName} in windowService`); + } } const unregisterContextMenu = await menuService.initContextMenuForWindowWebContents(newWindow.webContents);