forked from thomasdondorf/puppeteer-cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error-handling.js
38 lines (30 loc) · 1.21 KB
/
error-handling.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// This example explains the error handling
// Console output of the script will be:
// > Error crawling https://www.wikipedia.org/: Fake error
// > Error crawling https://domain.invalid/: net::ERR_NAME_NOT_RESOLVED at https://domain.invalid/
const { Cluster } = require('../dist');
(async () => {
// Create a cluster with 2 workers
const cluster = await Cluster.launch({
concurrency: Cluster.CONCURRENCY_CONTEXT,
maxConcurrency: 2,
});
// Event handler to be called in case of problems
cluster.on('taskerror', (err, data) => {
console.log(`Error crawling ${data}: ${err.message}`);
});
await cluster.task(async ({ page, data: url }) => {
// Simulate an error for Wikipedia
if (url === 'https://www.wikipedia.org/') {
throw new Error('Fake error');
}
await page.goto(url, { waitUntil: 'domcontentloaded' });
// ...
});
cluster.queue('https://www.google.com/');
cluster.queue('https://www.wikipedia.org/'); // error (see task function)
cluster.queue('https://github.com/');
cluster.queue('https://domain.invalid/'); // error: ERR_NAME_NOT_RESOLVED
await cluster.idle();
await cluster.close();
})();