From e58a03c84f89a5ec9091ca81ef040df659700ef2 Mon Sep 17 00:00:00 2001 From: Kitsune Ral Date: Tue, 29 May 2018 16:38:10 +0900 Subject: BaseJob: "background" switch; more extensive error reporting Running a request in background, aside from some tweaks on the network layer (see QNetworkRequest::BackgroundRequestAttribute), allows to distinguish jobs not immediately caused by user interaction (such as fetching thumbnails). This can be used to show or not show certain notifications in UI of clients. Error reporting has been extended with more methods: errorCaption() - a human-readable phrase calculated from the status code; intended to be shown as a dialog caption and in similar situations. errorRawData() - former errorDetails(), returns the raw response from the server. errorUrl() - returns a URL that may be useful with the error (e.g. for the upcoming "consent not given" error, this will have the policy URL). Connection::resultFailed() - a new signal emitted when _any_ BaseJob::failure() is emitted (enables centralised error handling across all network requests in clients). As a part of matching changes in Connection, callApi has an overload that allows to specify the policy; a custom enum instead of bool has been chosen for the parameter type, to avoid clashes with (arbitrary) types of job parameters. --- lib/connection.cpp | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) (limited to 'lib/connection.cpp') diff --git a/lib/connection.cpp b/lib/connection.cpp index 84557224..572ac76b 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -196,7 +196,7 @@ void Connection::doConnectToServer(const QString& user, const QString& password, }); connect(loginJob, &BaseJob::failure, this, [this, loginJob] { - emit loginError(loginJob->errorString(), loginJob->errorDetails()); + emit loginError(loginJob->errorString(), loginJob->errorRawData()); }); } @@ -263,7 +263,7 @@ void Connection::sync(int timeout) // Raw string: http://en.cppreference.com/w/cpp/language/string_literal const QString filter { R"({"room": { "timeline": { "limit": 100 } } })" }; auto job = d->syncJob = - callApi(d->data->lastEvent(), filter, timeout); + callApi(InBackground, d->data->lastEvent(), filter, timeout); connect( job, &SyncJob::success, this, [this, job] { onSyncSuccess(job->takeData()); d->syncJob = nullptr; @@ -272,15 +272,19 @@ void Connection::sync(int timeout) connect( job, &SyncJob::retryScheduled, this, [this,job] (int retriesTaken, int nextInMilliseconds) { - emit networkError(job->errorString(), job->errorDetails(), + emit networkError(job->errorString(), job->errorRawData(), retriesTaken, nextInMilliseconds); }); connect( job, &SyncJob::failure, this, [this, job] { d->syncJob = nullptr; if (job->error() == BaseJob::ContentAccessError) - emit loginError(job->errorString(), job->errorDetails()); + { + qCWarning(SYNCJOB) + << "Sync job failed with ContentAccessError - login expired?"; + emit loginError(job->errorString(), job->errorRawData()); + } else - emit syncError(job->errorString(), job->errorDetails()); + emit syncError(job->errorString(), job->errorRawData()); }); } @@ -386,22 +390,24 @@ inline auto splitMediaId(const QString& mediaId) return idParts; } -MediaThumbnailJob* Connection::getThumbnail(const QString& mediaId, QSize requestedSize) const +MediaThumbnailJob* Connection::getThumbnail(const QString& mediaId, + QSize requestedSize, RunningPolicy policy) const { auto idParts = splitMediaId(mediaId); - return callApi(idParts.front(), idParts.back(), - requestedSize); + return callApi(policy, + idParts.front(), idParts.back(), requestedSize); } -MediaThumbnailJob* Connection::getThumbnail(const QUrl& url, QSize requestedSize) const +MediaThumbnailJob* Connection::getThumbnail(const QUrl& url, + QSize requestedSize, RunningPolicy policy) const { - return getThumbnail(url.authority() + url.path(), requestedSize); + return getThumbnail(url.authority() + url.path(), requestedSize, policy); } -MediaThumbnailJob* Connection::getThumbnail(const QUrl& url, int requestedWidth, - int requestedHeight) const +MediaThumbnailJob* Connection::getThumbnail(const QUrl& url, + int requestedWidth, int requestedHeight, RunningPolicy policy) const { - return getThumbnail(url, QSize(requestedWidth, requestedHeight)); + return getThumbnail(url, QSize(requestedWidth, requestedHeight), policy); } UploadContentJob* Connection::uploadContent(QIODevice* contentSource, -- cgit v1.2.3