How to get detailed error message when QTWebKit fails to load a page?

前端 未结 1 510
暖寄归人
暖寄归人 2021-01-02 02:49

QtWebKit calls QWebPage::loadFinished ( false ) when a web page failed to load - but gives no clue as to why it failed.

How do I get a detailed error me

相关标签:
1条回答
  • 2021-01-02 03:28

    It turns out there are a couple ways to get more detail about failures:

    • Implement the onResourceRequested and onResourceReceived callbacks on page:

      page.onResourceRequested = function (resource) {
          log('resource requested: ' + resource.url);
      }
      
      page.onResourceReceived = function (resource) {
          log('resource received: ' + resource.status + ' ' + resource.statusText + ' ' +
              resource.contentType + ' ' + resource.url);
      }
      
    • If you are looking for more detail still, you need to patch PhantomJS internals. Update its CustomPage object (in WebPage.cpp) to implement QTWebKit's ErrorExtension. Here is code you can add that does that:

      protected: 
        bool supportsExtension(Extension extension) const {
          if (extension == QWebPage::ErrorPageExtension)
          {
              return true;
          }
          return false;
      }
      
      bool extension(Extension extension, const ExtensionOption *option = 0, ExtensionReturn *output = 0)
      {
          if (extension != QWebPage::ErrorPageExtension)
              return false;
      
          ErrorPageExtensionOption *errorOption = (ErrorPageExtensionOption*) option;
          std::cerr << "Error loading " << qPrintable(errorOption->url.toString())  << std::endl;
          if(errorOption->domain == QWebPage::QtNetwork)
              std::cerr << "Network error (" << errorOption->error << "): ";
          else if(errorOption->domain == QWebPage::Http)
              std::cerr << "HTTP error (" << errorOption->error << "): ";
          else if(errorOption->domain == QWebPage::WebKit)
              std::cerr << "WebKit error (" << errorOption->error << "): ";
      
          std::cerr << qPrintable(errorOption->errorString) << std::endl;
      
          return false;
      }
      

    This will give you most of the error information, but you can still get onLoadFinished(success=false) events without getting more detail. From my research, the primary cause of those is canceled load requests. QTWebKit sends a fail notification for cancellations, but doesn't report any detail.

    0 讨论(0)
提交回复
热议问题