Skip to content
This repository
Browse code

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...
commit fd700b0702f96cf9c78b3b0eaf9175c1e5967392 1 parent d906bc3
Vitaly Slobodin Vitallium authored committed
2  src/modules/webpage.js
@@ -251,6 +251,8 @@ function decorateNewPage(opts, page) {
251 251 definePageSignalHandler(page, handlers, "onResourceRequested", "resourceRequested");
252 252
253 253 definePageSignalHandler(page, handlers, "onResourceReceived", "resourceReceived");
  254 +
  255 + definePageSignalHandler(page, handlers, "onResourceError", "resourceError");
254 256
255 257 definePageSignalHandler(page, handlers, "onAlert", "javaScriptAlertSent");
256 258
17 src/networkaccessmanager.cpp
@@ -195,6 +195,7 @@ QNetworkReply *NetworkAccessManager::createRequest(Operation op, const QNetworkR
195 195
196 196 connect(reply, SIGNAL(readyRead()), this, SLOT(handleStarted()));
197 197 connect(reply, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(handleSslErrors(const QList<QSslError> &)));
  198 + connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(handleNetworkError()));
198 199
199 200 emit resourceRequested(data);
200 201 return reply;
@@ -296,3 +297,19 @@ void NetworkAccessManager::handleSslErrors(const QList<QSslError> &errors)
296 297 if (m_ignoreSslErrors)
297 298 reply->ignoreSslErrors();
298 299 }
  300 +
  301 +void NetworkAccessManager::handleNetworkError()
  302 +{
  303 + QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
  304 + qDebug() << "Network - Resource request error:"
  305 + << reply->error()
  306 + << "(" << reply->errorString() << ")";
  307 +
  308 + m_ids.remove(reply);
  309 +
  310 + if (m_started.contains(reply))
  311 + m_started.remove(reply);
  312 +
  313 + emit resourceError(reply->error(), reply->errorString());
  314 + reply->deleteLater();
  315 +}
2  src/networkaccessmanager.h
@@ -67,12 +67,14 @@ class NetworkAccessManager : public QNetworkAccessManager
67 67 signals:
68 68 void resourceRequested(const QVariant& data);
69 69 void resourceReceived(const QVariant& data);
  70 + void resourceError(const QVariant& errorCode, const QVariant& errorString);
70 71
71 72 private slots:
72 73 void handleStarted();
73 74 void handleFinished(QNetworkReply *reply);
74 75 void provideAuthentication(QNetworkReply *reply, QAuthenticator *authenticator);
75 76 void handleSslErrors(const QList<QSslError> &errors);
  77 + void handleNetworkError();
76 78
77 79 private:
78 80 QHash<QNetworkReply*, int> m_ids;
2  src/webpage.cpp
@@ -368,6 +368,8 @@ WebPage::WebPage(QObject *parent, const QUrl &baseUrl)
368 368 SIGNAL(resourceRequested(QVariant)));
369 369 connect(m_networkAccessManager, SIGNAL(resourceReceived(QVariant)),
370 370 SIGNAL(resourceReceived(QVariant)));
  371 + connect(m_networkAccessManager, SIGNAL(resourceError(QVariant, QVariant)),
  372 + SIGNAL(resourceError(QVariant, QVariant)));
371 373
372 374 m_customWebPage->setViewportSize(QSize(400, 300));
373 375 }
1  src/webpage.h
@@ -463,6 +463,7 @@ public slots:
463 463 void javaScriptErrorSent(const QString &msg, const QString &stack);
464 464 void resourceRequested(const QVariant &req);
465 465 void resourceReceived(const QVariant &resource);
  466 + void resourceError(const QVariant &errorCode, const QVariant &errorString);
466 467 void urlChanged(const QUrl &url);
467 468 void navigationRequested(const QUrl &url, const QString &navigationType, bool navigationLocked, bool isMainFrame);
468 469 void rawPageCreated(QObject *page);
38 test/webpage-spec.js
@@ -213,6 +213,7 @@ describe("WebPage object", function() {
213 213 expectHasFunction(page, 'render');
214 214 expectHasFunction(page, 'resourceReceived');
215 215 expectHasFunction(page, 'resourceRequested');
  216 + expectHasFunction(page, 'resourceError');
216 217 expectHasFunction(page, 'uploadFile');
217 218 expectHasFunction(page, 'sendEvent');
218 219 expectHasFunction(page, 'childFramesCount');
@@ -1042,6 +1043,43 @@ describe("WebPage object", function() {
1042 1043 expect(handled).toEqual(true);
1043 1044 });
1044 1045 });
  1046 +
  1047 + it('should handle resource request errors', function() {
  1048 + var server = require('webserver').create();
  1049 + var page = require('webpage').create();
  1050 +
  1051 + server.listen(12345, function(request, response) {
  1052 + if (request.url == '/notExistResource.png') {
  1053 + response.statusCode = 404;
  1054 + response.write('Not found!');
  1055 + response.close();
  1056 + } else {
  1057 + response.statusCode = 200;
  1058 + response.write('<html><body><img src="notExistResource.png"/></body></html>');
  1059 + response.close();
  1060 + }
  1061 + });
  1062 +
  1063 + var handled = false;
  1064 +
  1065 + runs(function() {
  1066 + page.onResourceError = function(errorCode, errorString) {
  1067 + expect(errorCode).toEqual(203);
  1068 + expect(errorString).toContain('notExistResource.png - server replied: Not Found');
  1069 + handled = true;
  1070 + };
  1071 +
  1072 + page.open('http://localhost:12345', function(status) {
  1073 + expect(status).toEqual('success');
  1074 + });
  1075 + });
  1076 +
  1077 + waits(5000);
  1078 +
  1079 + runs(function() {
  1080 + expect(handled).toEqual(true);
  1081 + });
  1082 + });
1045 1083 });
1046 1084
1047 1085 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.