{"record":{"id":"4bc0f442cba11d06","repo":"apify/crawlee","slug":"savesnapshot-with-key-key-failed-cause-err","errorCode":null,"errorMessage":"saveSnapshot with key ${key} failed.\nCause:${(err as Error).message}","messagePattern":"saveSnapshot with key (.+?) failed\\.\nCause:(.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/playwright-crawler/src/internals/utils/playwright-utils.ts","lineNumber":564,"sourceCode":"\n        if (saveScreenshot) {\n            const screenshotName = `${key}.jpg`;\n            const screenshotBuffer = await page.screenshot({\n                fullPage: true,\n                quality: screenshotQuality,\n                type: 'jpeg',\n                animations: 'disabled',\n            });\n            await store.setValue(screenshotName, screenshotBuffer, { contentType: 'image/jpeg' });\n        }\n\n        if (saveHtml) {\n            const htmlName = `${key}.html`;\n            const html = await page.content();\n            await store.setValue(htmlName, html, { contentType: 'text/html' });\n        }\n    } catch (err) {\n        throw new Error(`saveSnapshot with key ${key} failed.\\nCause:${(err as Error).message}`);\n    }\n}\n\n/**\n * Returns Cheerio handle for `page.content()`, allowing to work with the data same way as with {@apilink CheerioCrawler}.\n *\n * **Example usage:**\n * ```javascript\n * const $ = await playwrightUtils.parseWithCheerio(page);\n * const title = $('title').text();\n * ```\n *\n * @param page Playwright [`Page`](https://playwright.dev/docs/api/class-page) object.\n * @param ignoreShadowRoots\n */\nexport async function parseWithCheerio(\n    page: Page,\n    ignoreShadowRoots = false,","sourceCodeStart":546,"sourceCodeEnd":582,"githubUrl":"https://github.com/apify/crawlee/blob/dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c/packages/playwright-crawler/src/internals/utils/playwright-utils.ts#L546-L582","documentation":"saveSnapshot captures a page screenshot and/or HTML into a key-value store. Any failure during page.screenshot(), page.content(), or store.setValue() is caught and re-thrown wrapped in this error that names the snapshot key and the underlying cause, so failures are attributable to a specific snapshot.","triggerScenarios":"Calling context.saveSnapshot() (or saveSnapshot util) when the page is closed/crashed mid-navigation, the screenshot cannot be taken (invalid CDP state), or the key-value store is unavailable/read-only.","commonSituations":"Snapshot taken after navigation destroyed the page; storage quota or permissions issues in managed environments; concurrent setValue on the same key; saving snapshots of pages closed by a timeout.","solutions":["Read the `Cause:` suffix of the message to identify the underlying error and fix it (page state, storage, permissions).","Guard saveSnapshot calls: check page.isClosed() (or !page.isClosed()) before snapshotting.","Wrap snapshot calls in try/catch when snapshots are best-effort and should not fail the request.","Use unique snapshot keys (e.g., include request.id) to avoid store conflicts."],"exampleFix":"// before\nawait context.saveSnapshot({ key: 'snapshot' });\n// after\ntry {\n    if (!context.page.isClosed()) {\n        await context.saveSnapshot({ key: `snapshot-${context.request.id}` });\n    }\n} catch (err) {\n    log.warning(`Snapshot skipped: ${(err as Error).message}`);\n}","handlingStrategy":"try-catch","validationCode":"// pre-checks before snapshot\nif (!page || page.isClosed()) return; // skip snapshot","typeGuard":"function canSnapshot(page: Page | undefined): page is Page {\n    return !!page && !page.isClosed();\n}","tryCatchPattern":"try {\n    await context.saveSnapshot({ key: `snap-${context.request.id}` });\n} catch (err) {\n    log.warning(`Snapshot failed, continuing: ${(err as Error).message}`); // cause is embedded after 'Cause:'\n}","preventionTips":["Check page.isClosed() before snapshotting.","Treat snapshots as best-effort: catch and log instead of failing the request.","Use unique keys per request to avoid store conflicts."],"tags":["snapshot","key-value-store","playwright","crawlee"],"backgroundTag":"snapshot-save-failed","analyzedSha":"dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c","analyzedAt":"2026-08-30T22:22:28.328Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}