From ac1ab3b9869da387eda0d5d3345c3df3f9463a04 Mon Sep 17 00:00:00 2001 From: Rohit Rajan Date: Tue, 2 Jun 2026 11:39:32 +0530 Subject: [PATCH] fix: error and crash handling browser --- .../classes/RemoteBrowser.ts | 78 +++++++++++-------- server/src/utils/output-post-processor.ts | 2 + 2 files changed, 48 insertions(+), 32 deletions(-) diff --git a/server/src/browser-management/classes/RemoteBrowser.ts b/server/src/browser-management/classes/RemoteBrowser.ts index ac3a5655..9283e074 100644 --- a/server/src/browser-management/classes/RemoteBrowser.ts +++ b/server/src/browser-management/classes/RemoteBrowser.ts @@ -253,49 +253,63 @@ export class RemoteBrowser { } page.on('framenavigated', async (frame) => { - if (frame === page.mainFrame()) { - const currentUrl = page.url(); - if (this.shouldEmitUrlChange(currentUrl)) { - this.lastEmittedUrl = currentUrl; - this.broadcast('urlChanged', { url: currentUrl, userId: this.userId }); - } - - await page.evaluate(() => { - if (window.rrweb && window.isRecording) { - window.isRecording = false; + try { + if (frame === page.mainFrame()) { + if (page.isClosed()) { + return; } - }); - if (this.isRecordingMode) { - await page.waitForLoadState('networkidle', { timeout: 10000 }).catch(() => { - logger.warn('[rrweb] Network idle timeout on navigation, proceeding with rrweb initialization'); - }); + const currentUrl = page.url(); + if (this.shouldEmitUrlChange(currentUrl)) { + this.lastEmittedUrl = currentUrl; + this.broadcast('urlChanged', { url: currentUrl, userId: this.userId }); + } - await this.initializeRRWebRecording(page); + await page.evaluate(() => { + if (window.rrweb && window.isRecording) { + window.isRecording = false; + } + }).catch(() => {}); + + if (this.isRecordingMode && !page.isClosed()) { + await page.waitForLoadState('networkidle', { timeout: 10000 }).catch(() => { + logger.warn('[rrweb] Network idle timeout on navigation, proceeding with rrweb initialization'); + }); + + await this.initializeRRWebRecording(page).catch((error: any) => { + logger.warn(`[rrweb] Failed to initialize recording on navigation: ${error?.message}`); + }); + } } + } catch (error: any) { + logger.warn(`Error handling framenavigated event: ${error?.message}`); } }); page.on('load', async () => { - const injectScript = async (): Promise => { - try { - await page.waitForLoadState('networkidle', { timeout: 5000 }); + try { + const injectScript = async (): Promise => { + try { + await page.waitForLoadState('networkidle', { timeout: 5000 }); - if (page.isClosed()) { - logger.debug('Page is closed, cannot inject script'); + if (page.isClosed()) { + logger.debug('Page is closed, cannot inject script'); + return false; + } + + await page.evaluate(getInjectableScript()); + return true; + } catch (error: any) { + logger.log('warn', `Script injection attempt failed: ${error.message}`); return false; - } + } + }; - await page.evaluate(getInjectableScript()); - return true; - } catch (error: any) { - logger.log('warn', `Script injection attempt failed: ${error.message}`); - return false; - } - }; - - const success = await injectScript(); - console.log("Script injection result:", success); + const success = await injectScript(); + console.log("Script injection result:", success); + } catch (error: any) { + logger.warn(`Error handling page load event: ${error?.message}`); + } }); } diff --git a/server/src/utils/output-post-processor.ts b/server/src/utils/output-post-processor.ts index 37b67df1..86e35419 100644 --- a/server/src/utils/output-post-processor.ts +++ b/server/src/utils/output-post-processor.ts @@ -71,6 +71,7 @@ export async function processRobotOutputFormats( delete pageResult.html; } if (!effectiveFormats.includes('text')) delete pageResult.text; + if (!effectiveFormats.includes('links')) delete pageResult.links; const pageUrl = pageResult.metadata?.url || pageResult.url; const hasPageUrl = typeof pageUrl === 'string' && pageUrl.trim() !== ''; @@ -141,6 +142,7 @@ export async function processRobotOutputFormats( delete result.html; } if (!effectiveFormats.includes('text')) delete result.text; + if (!effectiveFormats.includes('links')) delete result.links; const resultUrl = result.metadata?.url || result.url; const hasResultUrl = typeof resultUrl === 'string' && resultUrl.trim() !== '';