From cc4922479ba7de93b55570b70ce36e4d09c2217e Mon Sep 17 00:00:00 2001 From: hamed musallam Date: Thu, 19 Feb 2026 10:04:01 +0100 Subject: [PATCH] fix: improve error handling for spectra snapshot capture close #102 --- .../nmr-cli/src/parse/prase-spectra.ts | 98 +++++++++---------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/app/scripts/nmr-cli/src/parse/prase-spectra.ts b/app/scripts/nmr-cli/src/parse/prase-spectra.ts index 8fe0247..c40cf3e 100644 --- a/app/scripts/nmr-cli/src/parse/prase-spectra.ts +++ b/app/scripts/nmr-cli/src/parse/prase-spectra.ts @@ -23,8 +23,9 @@ const parsingOptions: ParsingOptions = { }; interface Snapshot { - image: string - id: string + id: string; + image: string | null; + error: string | null; } const core = init() @@ -36,70 +37,69 @@ function generateNMRiumURL() { return url.toString() } -async function captureSpectraViewAsBase64(nmriumState: Partial) { - const { data: { spectra } = { spectra: [] }, version } = nmriumState - const browser = await playwright.chromium.launch() - const context = await browser.newContext( - playwright.devices['Desktop Chrome HiDPI'] - ) - const page = await context.newPage() - - const url = generateNMRiumURL() +async function launchBrowser() { + return playwright.chromium.launch(); +} - await page.goto(url) +async function captureSpectraViewAsBase64(nmriumState: Partial): Promise { + const { data: { spectra } = { spectra: [] }, version } = nmriumState; - await page.locator('text=Loading').waitFor({ state: 'hidden' }) + if (!spectra?.length) return []; - let snapshots: Snapshot[] = [] + const url = generateNMRiumURL(); + const snapshots: Snapshot[] = []; + let browser = await launchBrowser(); - for (const spectrum of spectra || []) { - const spectrumObject = { - version, - data: { - spectra: [{ ...spectrum }], - }, - } + for (const spectrum of spectra) { + let context = null; - // convert typed array to array - const stringObject = JSON.stringify( - spectrumObject, - (key, value: unknown) => { - return ArrayBuffer.isView(value) - ? Array.from(value as unknown as Iterable) - : value + try { + // recreate browser if it has crashed + if (!browser.isConnected()) { + browser = await launchBrowser(); } - ) - // load the spectrum into NMRium using the custom event - await page.evaluate( - ` - window.postMessage({ type: "nmr-wrapper:load", data:{data: ${stringObject},type:"nmrium"}}, '*'); - ` - ) + context = await browser.newContext(playwright.devices['Desktop Chrome HiDPI']); + const page = await context.newPage(); - //wait for NMRium process and load spectra - await page.locator('text=Loading').waitFor({ state: 'hidden' }) + await page.goto(url); + await page.locator('text=Loading').waitFor({ state: 'hidden' }); - // take a snapshot for the spectrum - try { - const snapshot = await page.locator('#nmrSVG .container').screenshot() + const stringObject = JSON.stringify( + { version, data: { spectra: [{ ...spectrum }] } }, + (key, value: unknown) => ArrayBuffer.isView(value) ? Array.from(value as unknown as Iterable) : value + ); + + await page.evaluate(` + window.postMessage({ type: "nmr-wrapper:load", data: { data: ${stringObject}, type: "nmrium" } }, '*'); + `); + + await page.locator('text=Loading').waitFor({ state: 'hidden' }); + const snapshot = await page.locator('#nmrSVG .container').screenshot(); + snapshots.push({ id: spectrum.id, image: snapshot.toString('base64'), error: null }); + + } catch (e) { snapshots.push({ - image: snapshot.toString('base64'), id: spectrum.id, - }) - } catch (e) { - console.log(e) - } - } + image: null, + error: e instanceof Error ? e.message : String(e), + }); - await context.close() - await browser.close() + // browser crashed — close and recreate for next spectrum + await browser.close().catch(() => { }); + browser = await launchBrowser(); - return snapshots + } finally { + await context?.close().catch(() => { }); + } + } + await browser.close().catch(() => { }); + return snapshots; } + interface ProcessSpectraOptions { autoDetection: boolean; autoProcessing: boolean; }