Reenable Retry button for parsing errors

This commit is contained in:
GriffinR 2024-02-05 11:54:35 -05:00
parent 582fb101cf
commit 5def0e8be1
3 changed files with 17 additions and 18 deletions

View file

@ -37,8 +37,7 @@ private:
void resetDialog(); void resetDialog();
void get(const QUrl &url); void get(const QUrl &url);
void processWebpage(const QJsonDocument &data, const QUrl &nextUrl); void processWebpage(const QJsonDocument &data, const QUrl &nextUrl);
void disableRequestsUntil(const QDateTime time); void error(const QString &err, const QDateTime time = QDateTime());
void error(const QString &err);
bool isNewerVersion(int major, int minor, int patch); bool isNewerVersion(int major, int minor, int patch);
private slots: private slots:

View file

@ -147,7 +147,7 @@ void NetworkAccessManager::processReply(QNetworkReply * reply, NetworkReplyData
this->cache.insert(url, cacheEntry); this->cache.insert(url, cacheEntry);
} }
auto eTagHeader = reply->header(QNetworkRequest::ETagHeader); auto eTagHeader = reply->header(QNetworkRequest::ETagHeader);
if (eTagHeader.isValid()) if (eTagHeader.canConvert<QString>())
cacheEntry->eTag = eTagHeader.toString(); cacheEntry->eTag = eTagHeader.toString();
cacheEntry->data = data->m_body = reply->readAll(); cacheEntry->data = data->m_body = reply->readAll();

View file

@ -69,8 +69,7 @@ void UpdatePromoter::get(const QUrl &url) {
auto reply = this->manager->get(url); auto reply = this->manager->get(url);
connect(reply, &NetworkReplyData::finished, [this, reply] () { connect(reply, &NetworkReplyData::finished, [this, reply] () {
if (!reply->errorString().isEmpty()) { if (!reply->errorString().isEmpty()) {
this->error(reply->errorString()); this->error(reply->errorString(), reply->retryAfter());
this->disableRequestsUntil(reply->retryAfter());
} else { } else {
this->processWebpage(QJsonDocument::fromJson(reply->body()), reply->nextUrl()); this->processWebpage(QJsonDocument::fromJson(reply->body()), reply->nextUrl());
} }
@ -80,7 +79,6 @@ void UpdatePromoter::get(const QUrl &url) {
// Read all the items on the releases page, ignoring entries without a version identifier tag. // Read all the items on the releases page, ignoring entries without a version identifier tag.
// Objects in the releases page data are sorted newest to oldest. // Objects in the releases page data are sorted newest to oldest.
// Returns true when finished, returns false to request processing for the next page.
void UpdatePromoter::processWebpage(const QJsonDocument &data, const QUrl &nextUrl) { void UpdatePromoter::processWebpage(const QJsonDocument &data, const QUrl &nextUrl) {
const QJsonArray releases = data.array(); const QJsonArray releases = data.array();
int i; int i;
@ -156,21 +154,23 @@ void UpdatePromoter::processWebpage(const QJsonDocument &data, const QUrl &nextU
} }
} }
void UpdatePromoter::disableRequestsUntil(const QDateTime time) { void UpdatePromoter::error(const QString &err, const QDateTime retryAfter) {
this->button_Retry->setEnabled(false);
auto timeUntil = QDateTime::currentDateTime().msecsTo(time);
if (timeUntil < 0) timeUntil = 0;
QTimer::singleShot(timeUntil, Qt::VeryCoarseTimer, [this]() {
this->button_Retry->setEnabled(true);
});
}
void UpdatePromoter::error(const QString &err) {
const QString message = QString("Failed to check for version update: %1").arg(err); const QString message = QString("Failed to check for version update: %1").arg(err);
ui->label_Status->setText(message); ui->label_Status->setText(message);
if (!this->isVisible()) if (!this->isVisible())
logWarn(message); logWarn(message);
// If a "retry after" date/time is provided, disable the Retry button until then.
// Otherwise users are allowed to retry after an error.
auto timeUntil = QDateTime::currentDateTime().msecsTo(retryAfter);
if (timeUntil > 0) {
this->button_Retry->setEnabled(false);
QTimer::singleShot(timeUntil, Qt::VeryCoarseTimer, [this]() {
this->button_Retry->setEnabled(true);
});
} else {
this->button_Retry->setEnabled(true);
}
} }
bool UpdatePromoter::isNewerVersion(int major, int minor, int patch) { bool UpdatePromoter::isNewerVersion(int major, int minor, int patch) {