Skip to content
Browse files

New WebPage callback: onResourceError.

This callback is invoked when the WebPage was unable to request the resource

Issue: http://code.google.com/p/phantomjs/issues/detail?id=954
  • Loading branch information...
1 parent d906bc3 commit fd700b0702f96cf9c78b3b0eaf9175c1e5967392 @Vitallium Vitallium committed with Dec 31, 2012
Showing with 62 additions and 0 deletions.
  1. +2 −0 src/modules/webpage.js
  2. +17 −0 src/networkaccessmanager.cpp
  3. +2 −0 src/networkaccessmanager.h
  4. +2 −0 src/webpage.cpp
  5. +1 −0 src/webpage.h
  6. +38 −0 test/webpage-spec.js
View
2 src/modules/webpage.js
@@ -251,6 +251,8 @@ function decorateNewPage(opts, page) {
definePageSignalHandler(page, handlers, "onResourceRequested", "resourceRequested");
definePageSignalHandler(page, handlers, "onResourceReceived", "resourceReceived");
+
+ definePageSignalHandler(page, handlers, "onResourceError", "resourceError");
definePageSignalHandler(page, handlers, "onAlert", "javaScriptAlertSent");
View
17 src/networkaccessmanager.cpp
@@ -195,6 +195,7 @@ QNetworkReply *NetworkAccessManager::createRequest(Operation op, const QNetworkR
connect(reply, SIGNAL(readyRead()), this, SLOT(handleStarted()));
connect(reply, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(handleSslErrors(const QList<QSslError> &)));
+ connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(handleNetworkError()));
emit resourceRequested(data);
return reply;
@@ -296,3 +297,19 @@ void NetworkAccessManager::handleSslErrors(const QList<QSslError> &errors)
if (m_ignoreSslErrors)
reply->ignoreSslErrors();
}
+
+void NetworkAccessManager::handleNetworkError()
+{
+ QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
+ qDebug() << "Network - Resource request error:"
+ << reply->error()
+ << "(" << reply->errorString() << ")";
+
+ m_ids.remove(reply);
+
+ if (m_started.contains(reply))
+ m_started.remove(reply);
+
+ emit resourceError(reply->error(), reply->errorString());
+ reply->deleteLater();
+}
View
2 src/networkaccessmanager.h
@@ -67,12 +67,14 @@ class NetworkAccessManager : public QNetworkAccessManager
signals:
void resourceRequested(const QVariant& data);
void resourceReceived(const QVariant& data);
+ void resourceError(const QVariant& errorCode, const QVariant& errorString);
private slots:
void handleStarted();
void handleFinished(QNetworkReply *reply);
void provideAuthentication(QNetworkReply *reply, QAuthenticator *authenticator);
void handleSslErrors(const QList<QSslError> &errors);
+ void handleNetworkError();
private:
QHash<QNetworkReply*, int> m_ids;
View
2 src/webpage.cpp
@@ -368,6 +368,8 @@ WebPage::WebPage(QObject *parent, const QUrl &baseUrl)
SIGNAL(resourceRequested(QVariant)));
connect(m_networkAccessManager, SIGNAL(resourceReceived(QVariant)),
SIGNAL(resourceReceived(QVariant)));
+ connect(m_networkAccessManager, SIGNAL(resourceError(QVariant, QVariant)),
+ SIGNAL(resourceError(QVariant, QVariant)));
m_customWebPage->setViewportSize(QSize(400, 300));
}
View
1 src/webpage.h
@@ -463,6 +463,7 @@ public slots:
void javaScriptErrorSent(const QString &msg, const QString &stack);
void resourceRequested(const QVariant &req);
void resourceReceived(const QVariant &resource);
+ void resourceError(const QVariant &errorCode, const QVariant &errorString);
void urlChanged(const QUrl &url);
void navigationRequested(const QUrl &url, const QString &navigationType, bool navigationLocked, bool isMainFrame);
void rawPageCreated(QObject *page);
View
38 test/webpage-spec.js
@@ -213,6 +213,7 @@ describe("WebPage object", function() {
expectHasFunction(page, 'render');
expectHasFunction(page, 'resourceReceived');
expectHasFunction(page, 'resourceRequested');
+ expectHasFunction(page, 'resourceError');
expectHasFunction(page, 'uploadFile');
expectHasFunction(page, 'sendEvent');
expectHasFunction(page, 'childFramesCount');
@@ -1042,6 +1043,43 @@ describe("WebPage object", function() {
expect(handled).toEqual(true);
});
});
+
+ it('should handle resource request errors', function() {
+ var server = require('webserver').create();
+ var page = require('webpage').create();
+
+ server.listen(12345, function(request, response) {
+ if (request.url == '/notExistResource.png') {
+ response.statusCode = 404;
+ response.write('Not found!');
+ response.close();
+ } else {
+ response.statusCode = 200;
+ response.write('<html><body><img src="notExistResource.png"/></body></html>');
+ response.close();
+ }
+ });
+
+ var handled = false;
+
+ runs(function() {
+ page.onResourceError = function(errorCode, errorString) {
+ expect(errorCode).toEqual(203);
+ expect(errorString).toContain('notExistResource.png - server replied: Not Found');
+ handled = true;
+ };
+
+ page.open('http://localhost:12345', function(status) {
+ expect(status).toEqual('success');
+ });
+ });
+
+ waits(5000);
+
+ runs(function() {
+ expect(handled).toEqual(true);
+ });
+ });
});
describe("WebPage construction with options", function () {

0 comments on commit fd700b0

Please sign in to comment.
Something went wrong with that request. Please try again.