From bcd8028132e518d6f6601ea42bf5f86dd9d88cb3 Mon Sep 17 00:00:00 2001 From: nerix Date: Tue, 2 May 2023 23:22:04 +0200 Subject: [PATCH] Improve error messages when updater fails a download (#4594) Co-authored-by: Rasmus Karlsson --- CHANGELOG.md | 2 ++ src/common/NetworkRequest.cpp | 18 +++++++++++ src/common/NetworkRequest.hpp | 5 +++ src/singletons/Updates.cpp | 60 +++++++++++++++++++++++++---------- 4 files changed, 69 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 599b2fb3f..d6cd25895 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,10 @@ ## Slated for 2.4.3 +- Minor: Improved error messages when the updater fails a download. (#4594) - Bugfix: Fixed the menu warping on macOS on Qt6. (#4595) - Bugfix: Fixed link tooltips not showing unless the thumbnail setting was enabled. (#4597) +- Dev: Added the ability to control the `followRedirect` mode for requests. (#4594) ## 2.4.3 Beta diff --git a/src/common/NetworkRequest.cpp b/src/common/NetworkRequest.cpp index 6d66b9777..cfe0cd177 100644 --- a/src/common/NetworkRequest.cpp +++ b/src/common/NetworkRequest.cpp @@ -150,6 +150,24 @@ NetworkRequest NetworkRequest::multiPart(QHttpMultiPart *payload) && return std::move(*this); } +NetworkRequest NetworkRequest::followRedirects(bool on) && +{ + if (on) + { + this->data->request_.setAttribute( + QNetworkRequest::RedirectPolicyAttribute, + QNetworkRequest::NoLessSafeRedirectPolicy); + } + else + { + this->data->request_.setAttribute( + QNetworkRequest::RedirectPolicyAttribute, + QNetworkRequest::ManualRedirectPolicy); + } + + return std::move(*this); +} + NetworkRequest NetworkRequest::payload(const QByteArray &payload) && { this->data->payload_ = payload; diff --git a/src/common/NetworkRequest.hpp b/src/common/NetworkRequest.hpp index 384c49f31..85f34782a 100644 --- a/src/common/NetworkRequest.hpp +++ b/src/common/NetworkRequest.hpp @@ -61,6 +61,11 @@ public: NetworkRequest authorizeTwitchV5(const QString &clientID, const QString &oauthToken = QString()) &&; NetworkRequest multiPart(QHttpMultiPart *payload) &&; + /** + * This will change `RedirectPolicyAttribute`. + * `QNetworkRequest`'s defaults are used by default (Qt 5: no-follow, Qt 6: follow). + */ + NetworkRequest followRedirects(bool on) &&; void execute(); diff --git a/src/singletons/Updates.cpp b/src/singletons/Updates.cpp index 44e8b492b..00bc2a6e4 100644 --- a/src/singletons/Updates.cpp +++ b/src/singletons/Updates.cpp @@ -122,6 +122,18 @@ void Updates::installUpdates() }); }) .onSuccess([this](auto result) -> Outcome { + if (result.status() != 200) + { + auto *box = new QMessageBox( + QMessageBox::Information, "Chatterino Update", + QStringLiteral("The update couldn't be downloaded " + "(HTTP status %1).") + .arg(result.status())); + box->setAttribute(Qt::WA_DeleteOnClose); + box->exec(); + return Failure; + } + QByteArray object = result.getData(); auto filename = combinePath(getPaths()->miscDirectory, "update.zip"); @@ -171,6 +183,18 @@ void Updates::installUpdates() box->exec(); }) .onSuccess([this](auto result) -> Outcome { + if (result.status() != 200) + { + auto *box = new QMessageBox( + QMessageBox::Information, "Chatterino Update", + QStringLiteral("The update couldn't be downloaded " + "(HTTP status %1).") + .arg(result.status())); + box->setAttribute(Qt::WA_DeleteOnClose); + box->exec(); + return Failure; + } + QByteArray object = result.getData(); auto filePath = combinePath(getPaths()->miscDirectory, "Update.exe"); @@ -254,52 +278,56 @@ void Updates::checkForUpdates() NetworkRequest(url) .timeout(60000) .onSuccess([this](auto result) -> Outcome { - auto object = result.parseJson(); + const auto object = result.parseJson(); /// Version available on every platform - QJsonValue version_val = object.value("version"); + auto version = object["version"]; - if (!version_val.isString()) + if (!version.isString()) { this->setStatus_(SearchFailed); - qCDebug(chatterinoUpdate) << "error updating"; + qCDebug(chatterinoUpdate) + << "error checking version - missing 'version'" << object; return Failure; } #if defined Q_OS_WIN || defined Q_OS_MACOS /// Downloads an installer for the new version - QJsonValue updateExe_val = object.value("updateexe"); - if (!updateExe_val.isString()) + auto updateExeUrl = object["updateexe"]; + if (!updateExeUrl.isString()) { this->setStatus_(SearchFailed); - qCDebug(chatterinoUpdate) << "error updating"; + qCDebug(chatterinoUpdate) + << "error checking version - missing 'updateexe'" << object; return Failure; } - this->updateExe_ = updateExe_val.toString(); + this->updateExe_ = updateExeUrl.toString(); # ifdef Q_OS_WIN /// Windows portable - QJsonValue portable_val = object.value("portable_download"); - if (!portable_val.isString()) + auto portableUrl = object["portable_download"]; + if (!portableUrl.isString()) { this->setStatus_(SearchFailed); - qCDebug(chatterinoUpdate) << "error updating"; + qCDebug(chatterinoUpdate) + << "error checking version - missing 'portable_download'" + << object; return Failure; } - this->updatePortable_ = portable_val.toString(); + this->updatePortable_ = portableUrl.toString(); # endif #elif defined Q_OS_LINUX - QJsonValue updateGuide_val = object.value("updateguide"); - if (updateGuide_val.isString()) + QJsonValue updateGuide = object.value("updateguide"); + if (updateGuide.isString()) { - this->updateGuideLink_ = updateGuide_val.toString(); + this->updateGuideLink_ = updateGuide.toString(); } #else return Failure; #endif /// Current version - this->onlineVersion_ = version_val.toString(); + this->onlineVersion_ = version.toString(); /// Update available :) if (this->currentVersion_ != this->onlineVersion_)