如何处理UnhandledPromiseRejectionWarning?

时间:2019-10-13 20:08:56

标签: javascript node.js puppeteer

#!/usr/bin/env node
// vim: set noexpandtab tabstop=2:

const puppeteer = require('puppeteer');
const fs = require('fs').promises;

const cookies_json_file = process.argv[2];
const url = process.argv[3];

(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    const cookiesString = await fs.readFile(cookies_json_file);
    const cookies = JSON.parse(cookiesString);
    await page.setCookie.apply(page, cookies);

    //await page.goto(url);
    await page.goto(url, { waitUntil: 'networkidle2' });
    const content = await page.content();
    console.log(content);
    await browser.close();
})();

运行上面的代码时,出现以下错误。然后它挂在那里而没有终止。

(node:50025) UnhandledPromiseRejectionWarning: TimeoutError: Navigation Timeout Exceeded: 30000ms exceeded
    at /usr/local/lib/node_modules/puppeteer/lib/LifecycleWatcher.js:142:21
  -- ASYNC --
    at Frame.<anonymous> (/usr/local/lib/node_modules/puppeteer/lib/helper.js:110:27)
    at Page.goto (/usr/local/lib/node_modules/puppeteer/lib/Page.js:629:49)
    at Page.<anonymous> (/usr/local/lib/node_modules/puppeteer/lib/helper.js:111:23)
    at main.js:19:13
    at processTicksAndRejections (internal/process/task_queues.js:89:5)
(node:50025) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:50025) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

尽管有关于此消息的讨论,但我不能遵循它,也无法弄清楚如何修改此代码。有人可以告诉我如何解决此代码吗?谢谢。

https://thecodebarbarian.com/unhandled-promise-rejections-in-node.js.html

编辑:这是到目前为止的更新代码。

#!/usr/bin/env node
// vim: set noexpandtab tabstop=2:

const puppeteer = require('puppeteer');
const fs = require('fs').promises;

const cookies_json_file = process.argv[2];
const url = process.argv[3];

(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    const cookiesString = await fs.readFile(cookies_json_file);
    const cookies = JSON.parse(cookiesString);
    await page.setCookie.apply(page, cookies);

    try {
        await page.goto(url, { waitUntil: 'networkidle2' });
        const content = await page.content();
        console.log(content);
    } catch (e) {
        console.error(e);
        process.exit(1);
    } finally {
        await browser.close();
    }
})();

1 个答案:

答案 0 :(得分:0)

当前没有在puppeteer中禁用超时的方法。 建议您将{ timeout: 3000000 }添加到page.goto参数中。

编辑:我发现您想抓住错误,而不是减少错误发生的机会。

此代码应该有效

#!/usr/bin/env node
// vim: set noexpandtab tabstop=2:

const puppeteer = require('puppeteer');
const fs = require('fs').promises;

const cookies_json_file = process.argv[2];
const url = process.argv[3];

(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    const cookiesString = await fs.readFile(cookies_json_file);
    const cookies = JSON.parse(cookiesString);
    await page.setCookie.apply(page, cookies);

    try {
        await page.goto(url, { waitUntil: 'networkidle2' });

        const content = await page.content();
        console.log(content);
        await browser.close();
    } catch (e) {console.log(e)}
})();