diff --git a/packages/core/src/crawlers/error_snapshotter.ts b/packages/core/src/crawlers/error_snapshotter.ts index dc773da13720..48ef0209285a 100644 --- a/packages/core/src/crawlers/error_snapshotter.ts +++ b/packages/core/src/crawlers/error_snapshotter.ts @@ -18,6 +18,13 @@ export interface SnapshotResult { htmlFileName?: string; } +interface ErrorSnapshot { + screenshotFileName?: string; + screenshotFileUrl?: string; + htmlFileName?: string; + htmlFileUrl?: string; +} + /** * ErrorSnapshotter class is used to capture a screenshot of the page and a snapshot of the HTML when an error occurs during web crawling. * @@ -42,7 +49,7 @@ export class ErrorSnapshotter { /** * Capture a snapshot of the error context. */ - async captureSnapshot(error: ErrnoException, context: CrawlingContext): Promise<{ screenshotFileName?: string; htmlFileName?: string }> { + async captureSnapshot(error: ErrnoException, context: CrawlingContext): Promise { try { const page = context?.page as BrowserPage | undefined; const body = context?.body; @@ -80,7 +87,9 @@ export class ErrorSnapshotter { return { screenshotFileName, + screenshotFileUrl: screenshotFileName && keyValueStore.getPublicUrl(screenshotFileName), htmlFileName, + htmlFileUrl: htmlFileName && keyValueStore.getPublicUrl(htmlFileName), }; } catch { return {}; diff --git a/packages/core/src/crawlers/error_tracker.ts b/packages/core/src/crawlers/error_tracker.ts index e592ca84678c..c3b294a657a1 100644 --- a/packages/core/src/crawlers/error_tracker.ts +++ b/packages/core/src/crawlers/error_tracker.ts @@ -407,10 +407,12 @@ export class ErrorTracker { return; } - const { screenshotFileName, htmlFileName } = await this.errorSnapshotter.captureSnapshot(error, context); + const { screenshotFileName, htmlFileName, screenshotFileUrl, htmlFileUrl } = await this.errorSnapshotter.captureSnapshot(error, context); storage.firstErrorScreenshot = screenshotFileName; + storage.firstErrorScreenshotUrl = screenshotFileUrl; storage.firstErrorHtml = htmlFileName; + storage.firstErrorHtmlUrl = htmlFileUrl; } reset() { diff --git a/packages/core/src/storages/key_value_store.ts b/packages/core/src/storages/key_value_store.ts index 6167e9192ccf..12b157482bf3 100644 --- a/packages/core/src/storages/key_value_store.ts +++ b/packages/core/src/storages/key_value_store.ts @@ -428,6 +428,14 @@ export class KeyValueStore { : undefined; // [].forEach() returns undefined. } + /** + * Returns a file URL for the given key. + */ + getPublicUrl(key: string): string { + const name = this.name ?? this.config.get('defaultKeyValueStoreId'); + return `file://${process.cwd()}/storage/key_value_stores/${name}/${key}`; + } + /** * Opens a key-value store and returns a promise resolving to an instance of the {@apilink KeyValueStore} class. *