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

↘锁芯ラ 提交于 2019-11-30 12:17:56
Blake Scholl

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!