Collect updater errors and signal only on final failure

This commit is contained in:
Gres 2020-04-09 21:01:13 +03:00
parent a865381b65
commit 8bd89db060
2 changed files with 35 additions and 11 deletions

View File

@ -189,8 +189,10 @@ void Loader::startDownloadUpdates(const QUrl &previous)
url = updateUrls_[index + 1]; url = updateUrls_[index + 1];
} }
if (url.isEmpty()) if (url.isEmpty()) {
dumpErrors();
return; return;
}
auto reply = network_->get(QNetworkRequest(url)); auto reply = network_->get(QNetworkRequest(url));
if (reply->error() != QNetworkReply::NoError) if (reply->error() != QNetworkReply::NoError)
@ -203,14 +205,14 @@ void Loader::handleUpdateReply(QNetworkReply *reply)
const auto url = reply->url(); const auto url = reply->url();
if (reply->error() != QNetworkReply::NoError) { if (reply->error() != QNetworkReply::NoError) {
emit error(toError(*reply)); addError(toError(*reply));
startDownloadUpdates(url); startDownloadUpdates(url);
return; return;
} }
const auto replyData = reply->readAll(); const auto replyData = reply->readAll();
if (replyData.isEmpty()) { if (replyData.isEmpty()) {
emit error(tr("Received empty updates info from %1").arg(url.toString())); addError(tr("Received empty updates info from %1").arg(url.toString()));
startDownloadUpdates(url); startDownloadUpdates(url);
return; return;
} }
@ -219,7 +221,7 @@ void Loader::handleUpdateReply(QNetworkReply *reply)
url.toString().endsWith(".zip") ? unpack(replyData) : replyData; url.toString().endsWith(".zip") ? unpack(replyData) : replyData;
if (unpacked.isEmpty()) { if (unpacked.isEmpty()) {
emit error( addError(
tr("Empty updates info after unpacking from %1").arg(url.toString())); tr("Empty updates info after unpacking from %1").arg(url.toString()));
startDownloadUpdates(url); startDownloadUpdates(url);
return; return;
@ -228,12 +230,14 @@ void Loader::handleUpdateReply(QNetworkReply *reply)
SOFT_ASSERT(model_, return ); SOFT_ASSERT(model_, return );
const auto parseError = model_->parse(unpacked); const auto parseError = model_->parse(unpacked);
if (!parseError.isEmpty()) { if (!parseError.isEmpty()) {
emit error(tr("Failed to parse updates from %1 (%2)") addError(tr("Failed to parse updates from %1 (%2)")
.arg(url.toString(), parseError)); .arg(url.toString(), parseError));
startDownloadUpdates(url); startDownloadUpdates(url);
return; return;
} }
errors_.clear();
if (model_->hasUpdates()) if (model_->hasUpdates())
emit updatesAvailable(); emit updatesAvailable();
} }
@ -308,7 +312,7 @@ bool Loader::handleComponentReply(QNetworkReply *reply)
downloads_.erase(reply); downloads_.erase(reply);
if (reply->error() != QNetworkReply::NoError) { if (reply->error() != QNetworkReply::NoError) {
emit error(toError(*reply)); addError(toError(*reply));
if (!startDownload(*file)) if (!startDownload(*file))
finishUpdate(); finishUpdate();
@ -326,7 +330,7 @@ bool Loader::handleComponentReply(QNetworkReply *reply)
const auto url = reply->url(); const auto url = reply->url();
const auto replyData = reply->readAll(); const auto replyData = reply->readAll();
if (replyData.isEmpty()) { if (replyData.isEmpty()) {
emit error(tr("Empty data downloaded from %1").arg(url.toString())); addError(tr("Empty data downloaded from %1").arg(url.toString()));
if (!startDownload(*file)) if (!startDownload(*file))
finishUpdate(); finishUpdate();
@ -339,7 +343,7 @@ bool Loader::handleComponentReply(QNetworkReply *reply)
const auto unpacked = mustUnpack ? unpack(replyData) : replyData; const auto unpacked = mustUnpack ? unpack(replyData) : replyData;
if (unpacked.isEmpty()) { if (unpacked.isEmpty()) {
emit error(tr("Empty data after unpacking from %1").arg(url.toString())); addError(tr("Empty data after unpacking from %1").arg(url.toString()));
if (!startDownload(*file)) if (!startDownload(*file))
finishUpdate(); finishUpdate();
@ -370,7 +374,8 @@ void Loader::finishUpdate(const QString &error)
for (const auto &i : downloads_) i.first->deleteLater(); for (const auto &i : downloads_) i.first->deleteLater();
downloads_.clear(); downloads_.clear();
if (!error.isEmpty()) if (!error.isEmpty())
emit this->error(error); addError(error);
dumpErrors();
SOFT_ASSERT(model_, return ); SOFT_ASSERT(model_, return );
model_->updateStates(); model_->updateStates();
} }
@ -381,9 +386,10 @@ void Loader::commitUpdate()
Installer installer(currentActions_); Installer installer(currentActions_);
if (installer.commit()) { if (installer.commit()) {
model_->resetProgress(); model_->resetProgress();
errors_.clear();
emit updated(); emit updated();
} else { } else {
emit error(tr("Update failed: %1").arg(installer.errorString())); addError(tr("Update failed: %1").arg(installer.errorString()));
} }
finishUpdate(); finishUpdate();
} }
@ -403,6 +409,21 @@ Model *Loader::model() const
return model_; return model_;
} }
void Loader::addError(const QString &text)
{
LTRACE() << text;
errors_.append(text);
}
void Loader::dumpErrors()
{
if (errors_.isEmpty())
return;
const auto summary = errors_.join('\n');
emit error(summary);
errors_.clear();
}
Model::Model(QObject *parent) Model::Model(QObject *parent)
: QAbstractItemModel(parent) : QAbstractItemModel(parent)
{ {

View File

@ -137,6 +137,8 @@ signals:
void error(const QString& error); void error(const QString& error);
private: private:
void addError(const QString& text);
void dumpErrors();
void handleReply(QNetworkReply* reply); void handleReply(QNetworkReply* reply);
bool handleComponentReply(QNetworkReply* reply); bool handleComponentReply(QNetworkReply* reply);
void handleUpdateReply(QNetworkReply* reply); void handleUpdateReply(QNetworkReply* reply);
@ -152,6 +154,7 @@ private:
Urls updateUrls_; Urls updateUrls_;
QString downloadPath_; QString downloadPath_;
std::map<QNetworkReply*, File*> downloads_; std::map<QNetworkReply*, File*> downloads_;
QStringList errors_;
UserActions currentActions_; UserActions currentActions_;
}; };