【问题标题】:how to handle ERR_CONNECTION_CLOSED error如何处理 ERR_CONNECTION_CLOSED 错误
【发布时间】:2019-09-11 18:34:06
【问题描述】:

所以我读到了一些错误和 https ERR_CONNECTION_CLOSED。我担心的是如何在 puppeteer 中处理这个问题?

我当前的问题是,如果发生此错误,puppeteer 会等待 10 分钟,直到它最终被抛出。浏览器只是显示等待 .... 它一直在等待。

我尝试了navigationtimeout,但这似乎没有奏效。无论如何要强制 puppeteer 更早停止,而不是无限期地等待页面?

【问题讨论】:

    标签: google-chrome chromium puppeteer


    【解决方案1】:

    由于 puppeteer 大量使用异步函数,因此使用 try...catch 来处理其错误是一种很好的方法。示例:

    // when inside an async function:
    
    // supose I have access to `browser` and `page` objects here, outside try...catch
    try {
      // do something with browser/page that may result in errors. Example:
      const response = await page.goto('https://www.google.com', { waitUntil: 'load', timeout: 10000 });
      // do something with the results...
    catch (error) {
      // if something in the "try" fails, you have access to `page` and `browser` objects here
      // and you can treat the errors, or simply log to console and close the page/browser. E.g.:
      if (error.message.includes('ERR_CONNECTION_CLOSED')) {
        console.log('Oopps...');
        await page.close();
        await browser.close();
      } else {
        // try again, maybe...
      }
    }
    

    如果您更喜欢使用传统的 .then() 和 .catch() 承诺方法,您可以做类似的事情:

    const puppeteer = require('puppeteer');
    puppeteer.launch()
      .then(browser => browser.newPage()
        .then(page => page.goto('https://aldfkjasldfj')
          .then(response => {
            // do something with the response if all is good
          })
          .catch(reason => {
            console.log(reason.message);
            console.log('closing the page and the browser');
            page.close().then(() => browser.close());
          })
        )
      );
    

    对上面的代码非常抱歉。您可以看到 .then() 和 .catch() 非常难看。 :) 我更喜欢 async/await 。

    【讨论】:

    • 问题是关闭连接需要 10 分钟,它只是尝试加载页面 10 分钟.....它应该在 30 秒后停止,因为这是默认超时。它似乎忽略了超时
    • 嗨,埃尔西德。那接缝很奇怪。能分享一下decode吗?您正在使用 page.setDefaultTimeout 吗?
    • Elsid.. 我记得我前一段时间有类似的东西。问题是打开 devtools(chrome “headed”)和使用 page.setRequestInterception(true) 允许/取消某些请求的组合。在这种情况下,我确信眨眼是错误的,并且如果只有一个站点,则只有 2 个 POST 申请时表现得很奇怪。这让我疯狂了好几天,最后我发现这种行为只发生在这个组合中:devtools + .setRequestInterception(true)。为了解决这个问题,我在 devtools 打开之后和开始导航之前添加了一点延迟(300 毫秒)。
    • [...continuing] 我很确定这是 Blink 的一个错误,并继续解决这个问题。无论如何......也许你正面临类似的事情。可能是??
    【解决方案2】:
    await page.goto("https://www.example.com" , {
        waitUntil: 'domcontentloaded'
    }).catch(err => {
        if(err.message.includes("ERR_PROXY_CONNECTION_FAILED") || err.message.includes("ERR_TUNNEL_CONNECTION_FAILED")){
            return "ERR_PROXY_FAILED";
        }else if(err.message.includes("Navigation Timeout")){
            return "ERR_SLOW_CONNECTION";
        }else if(err.message.includes("Protocol error")){
            return "ERR_UNKNOWN_PROTOCOL";
        }else{
            throw err;
        }
    }); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-19
      相关资源
      最近更新 更多