From 60e83eee94bbf31ebb5151232c750c88e589c3d7 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 22 Jul 2016 18:09:44 +0900 Subject: Separate errors of access denial kind to a different BaseJob error code as well + stability fixes 1. Introduce ContentAccessDenied error code to BaseJob to allow clients to treat access denial errors differently from other network errors. 2. Since parseJson() overrides are responsible for calling emitResult(), the "default" BaseJob::parseJson() should call emitResult() as well. 3. Make sure BaseJob::fail() doesn't crash in absence of QNetworkReply. --- jobs/basejob.cpp | 19 ++++++++++++++++--- jobs/basejob.h | 3 ++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/jobs/basejob.cpp b/jobs/basejob.cpp index 50c85048..2177fe9c 100644 --- a/jobs/basejob.cpp +++ b/jobs/basejob.cpp @@ -80,6 +80,7 @@ QUrlQuery BaseJob::query() const void BaseJob::parseJson(const QJsonDocument& data) { + emitResult(); } void BaseJob::start() @@ -120,7 +121,7 @@ void BaseJob::fail(int errorCode, QString errorString) { setError( errorCode ); setErrorText( errorString ); - if( d->reply->isRunning() ) + if( d->reply && d->reply->isRunning() ) d->reply->abort(); qWarning() << "Job" << objectName() << "failed:" << errorString; emitResult(); @@ -138,12 +139,24 @@ QNetworkReply* BaseJob::networkReply() const void BaseJob::gotReply() { - if( d->reply->error() != QNetworkReply::NoError ) + switch( d->reply->error() ) { - qDebug() << "NetworkError:" << d->reply->error(); + case QNetworkReply::NoError: + break; // All good, go to the normal flow after the switch() + + case QNetworkReply::AuthenticationRequiredError: + case QNetworkReply::ContentAccessDenied: + case QNetworkReply::ContentOperationNotPermittedError: + qDebug() << "Content access error, Qt error code:" << d->reply->error(); + fail( ContentAccessError, d->reply->errorString() ); + return; + + default: + qDebug() << "NetworkError, Qt error code:" << d->reply->error(); fail( NetworkError, d->reply->errorString() ); return; } + QJsonParseError error; QJsonDocument data = QJsonDocument::fromJson(d->reply->readAll(), &error); if( error.error != QJsonParseError::NoError ) diff --git a/jobs/basejob.h b/jobs/basejob.h index 98b7358b..150d39e8 100644 --- a/jobs/basejob.h +++ b/jobs/basejob.h @@ -47,7 +47,8 @@ namespace QMatrixClient void start() override; enum ErrorCode { NetworkError = KJob::UserDefinedError, - JsonParseError, TimeoutError, UserDefinedError }; + JsonParseError, TimeoutError, ContentAccessError, + UserDefinedError = 512 }; signals: /** -- cgit v1.2.3