Skip to content

Commit

Permalink
Additional exception safety (#692)
Browse files Browse the repository at this point in the history
- add additional catch() block
- wrap page.title() in timedRun() to catch/log exception if this fails
- log error in getting cookies
- hopefully fixes hard-to-repro edge case crash in openzim/zimit#376
  • Loading branch information
ikreymer committed Sep 27, 2024
1 parent 607fc84 commit a56e13d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
7 changes: 6 additions & 1 deletion src/crawler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,12 @@ self.__bx_behaviors.selectMainBehavior();
// run custom driver here
await this.driver({ page, data, crawler: this });

data.title = await page.title();
data.title = await timedRun(
page.title(),
PAGE_OP_TIMEOUT_SECS,
"Timed out getting page title, something is likely wrong",
logDetails,
);
data.favicon = await this.getFavicon(page, logDetails);

await this.doPostLoadActions(opts);
Expand Down
4 changes: 2 additions & 2 deletions src/util/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,8 @@ export class Browser {
}
}

addInitScript(page: Page, script: string) {
return page.evaluateOnNewDocument(script);
async addInitScript(page: Page, script: string) {
await page.evaluateOnNewDocument(script);
}

async checkScript(cdp: CDPSession, filename: string, script: string) {
Expand Down
19 changes: 12 additions & 7 deletions src/util/recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1249,14 +1249,19 @@ export class Recorder {
return { fetched, mime, ts };
}

async getCookieString(cdp: CDPSession, url: string) {
const cookieList: string[] = [];
const { cookies } = await cdp.send("Network.getCookies", { urls: [url] });
for (const { name, value } of cookies) {
cookieList.push(`${name}=${value}`);
}
async getCookieString(cdp: CDPSession, url: string): Promise<string> {
try {
const cookieList: string[] = [];
const { cookies } = await cdp.send("Network.getCookies", { urls: [url] });
for (const { name, value } of cookies) {
cookieList.push(`${name}=${value}`);
}

return cookieList.join(";");
return cookieList.join(";");
} catch (e) {
logger.warn("Error getting cookies", { page: url, e }, "recorder");
return "";
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/util/screencaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ class RedisPubSubTransport {
this.castChannel = `c:${crawlId}:cast`;
this.ctrlChannel = `c:${crawlId}:ctrl`;

void this.init(redisUrl);
this.init(redisUrl).catch((e) =>
logger.warn("error starting cast", e, "screencast"),
);
}

async init(redisUrl: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/util/timing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function timedRun(

return Promise.race([promise, rejectPromiseOnTimeout(timeout)]).catch(
(err) => {
if (err == "timeout reached") {
if (err === "timeout reached") {
const logFunc = isWarn ? logger.warn : logger.error;
logFunc.call(
logger,
Expand Down

0 comments on commit a56e13d

Please sign in to comment.