From f638c58545ee0a159cae3e91e1ccb3089945930e Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 6 May 2016 22:11:08 +0900 Subject: Add separate success() and failure() signals to BaseJob This is to facilitate processing of job results (see further commits). --- jobs/basejob.cpp | 7 +++++++ jobs/basejob.h | 13 ++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) (limited to 'jobs') diff --git a/jobs/basejob.cpp b/jobs/basejob.cpp index 519e1517..e24db012 100644 --- a/jobs/basejob.cpp +++ b/jobs/basejob.cpp @@ -42,6 +42,13 @@ class BaseJob::Private BaseJob::BaseJob(ConnectionData* connection, JobHttpType type, bool needsToken) : d(new Private(connection, type, needsToken)) { + // Work around KJob inability to separate success and failure signals + connect(this, &BaseJob::result, [this]() { + if (error() == NoError) + emit success(this); + else + emit failure(this); + }); } BaseJob::~BaseJob() diff --git a/jobs/basejob.h b/jobs/basejob.h index 95cf4232..f343c769 100644 --- a/jobs/basejob.h +++ b/jobs/basejob.h @@ -46,7 +46,18 @@ namespace QMatrixClient void start() override; enum ErrorCode { NetworkError = KJob::UserDefinedError, JsonParseError, UserDefinedError }; - + + signals: + /** + * Emitted together with KJob::result() but only if there's no error. + */ + void success(BaseJob*); + /** + * Emitted together with KJob::result() if there's an error. + * Same as result(), this won't be emitted in case of kill(Quietly). + */ + void failure(BaseJob*); + protected: ConnectionData* connection() const; -- cgit v1.2.3 From a3be7715910434cd8ef9d626b8c9c518e2d5fc40 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 6 May 2016 22:35:59 +0900 Subject: Make sure the QNetworkReply object is aborted at any failure and even silent destruction of the job --- jobs/basejob.cpp | 9 +++++++-- jobs/basejob.h | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) (limited to 'jobs') diff --git a/jobs/basejob.cpp b/jobs/basejob.cpp index e24db012..bcfd5875 100644 --- a/jobs/basejob.cpp +++ b/jobs/basejob.cpp @@ -54,7 +54,11 @@ BaseJob::BaseJob(ConnectionData* connection, JobHttpType type, bool needsToken) BaseJob::~BaseJob() { if( d->reply ) + { + if( d->reply->isRunning() ) + d->reply->abort(); d->reply->deleteLater(); + } delete d; } @@ -115,6 +119,8 @@ void BaseJob::fail(int errorCode, QString errorString) { setError( errorCode ); setErrorText( errorString ); + if( d->reply->isRunning() ) + d->reply->abort(); emitResult(); } @@ -149,8 +155,7 @@ void BaseJob::gotReply() void BaseJob::timeout() { qDebug() << "Timeout!"; - if( d->reply->isRunning() ) - d->reply->abort(); + fail( TimeoutError, "The job has timed out" ); } void BaseJob::sslErrors(const QList& errors) diff --git a/jobs/basejob.h b/jobs/basejob.h index f343c769..f1ad66d1 100644 --- a/jobs/basejob.h +++ b/jobs/basejob.h @@ -45,7 +45,8 @@ namespace QMatrixClient void start() override; - enum ErrorCode { NetworkError = KJob::UserDefinedError, JsonParseError, UserDefinedError }; + enum ErrorCode { NetworkError = KJob::UserDefinedError, + JsonParseError, TimeoutError, UserDefinedError }; signals: /** -- cgit v1.2.3 From c1de050c143a79361e6b183bbfa84fc2fdaef959 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Fri, 6 May 2016 21:26:37 +0900 Subject: Enhanced logging of job failures --- jobs/basejob.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'jobs') diff --git a/jobs/basejob.cpp b/jobs/basejob.cpp index bcfd5875..cfdf8a28 100644 --- a/jobs/basejob.cpp +++ b/jobs/basejob.cpp @@ -121,6 +121,7 @@ void BaseJob::fail(int errorCode, QString errorString) setErrorText( errorString ); if( d->reply->isRunning() ) d->reply->abort(); + qWarning() << this << "failed:" << errorString; emitResult(); } @@ -138,7 +139,7 @@ void BaseJob::gotReply() { if( d->reply->error() != QNetworkReply::NoError ) { - qDebug() << "NetworkError!!!" << d->reply->error(); + qDebug() << "NetworkError:" << d->reply->error(); fail( NetworkError, d->reply->errorString() ); return; } -- cgit v1.2.3