开发者

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

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

How do I get a detailed error message, like HTTP response code or other message?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜