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.
This commit is contained in:
lin onetwo 2026-01-21 15:43:55 +08:00
parent 91196ec853
commit 17a87e7cb4
3 changed files with 13 additions and 3 deletions

View file

@ -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);

View file

@ -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

View file

@ -22,6 +22,10 @@ export async function handleCreateBasicWindow<N extends WindowNames>(
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);