Skip to content

Commit

Permalink
fix: provide URLs to the error snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed May 21, 2024
1 parent 72ebcd9 commit 82d3691
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
11 changes: 10 additions & 1 deletion packages/core/src/crawlers/error_snapshotter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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<ErrorSnapshot> {
try {
const page = context?.page as BrowserPage | undefined;
const body = context?.body;
Expand Down Expand Up @@ -80,7 +87,9 @@ export class ErrorSnapshotter {

return {
screenshotFileName,
screenshotFileUrl: screenshotFileName && keyValueStore.getPublicUrl(screenshotFileName),
htmlFileName,
htmlFileUrl: htmlFileName && keyValueStore.getPublicUrl(htmlFileName),
};
} catch {
return {};
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/crawlers/error_tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/storages/key_value_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 82d3691

Please sign in to comment.