Don't use output parameters for error handling

This commit is contained in:
Vladimir Golovnev (Glassez) 2021-10-06 21:45:37 +03:00
commit 41fc0fd084
No known key found for this signature in database
GPG key ID: 52A2C7DEE2DFA6F7
12 changed files with 116 additions and 165 deletions

View file

@ -252,13 +252,13 @@ bool AddNewTorrentDialog::loadTorrentFile(const QString &torrentPath)
? QUrl::fromEncoded(torrentPath.toLocal8Bit()).toLocalFile()
: torrentPath;
QString error;
m_torrentInfo = BitTorrent::TorrentInfo::loadFromFile(decodedPath, &error);
if (!m_torrentInfo.isValid())
const nonstd::expected<BitTorrent::TorrentInfo, QString> result = BitTorrent::TorrentInfo::loadFromFile(decodedPath);
m_torrentInfo = result.value_or(BitTorrent::TorrentInfo());
if (!result)
{
RaisedMessageBox::critical(this, tr("Invalid torrent")
, tr("Failed to load the torrent: %1.\nError: %2", "Don't remove the '\n' characters. They insert a newline.")
.arg(Utils::Fs::toNativePath(decodedPath), error));
.arg(Utils::Fs::toNativePath(decodedPath), result.error()));
return false;
}
@ -746,36 +746,38 @@ void AddNewTorrentDialog::setupTreeview()
updateDiskSpaceLabel();
}
void AddNewTorrentDialog::handleDownloadFinished(const Net::DownloadResult &result)
void AddNewTorrentDialog::handleDownloadFinished(const Net::DownloadResult &downloadResult)
{
QString error;
switch (result.status)
switch (downloadResult.status)
{
case Net::DownloadStatus::Success:
m_torrentInfo = BitTorrent::TorrentInfo::load(result.data, &error);
if (!m_torrentInfo.isValid())
{
RaisedMessageBox::critical(this, tr("Invalid torrent"), tr("Failed to load from URL: %1.\nError: %2")
.arg(result.url, error));
return;
const nonstd::expected<BitTorrent::TorrentInfo, QString> result = BitTorrent::TorrentInfo::load(downloadResult.data);
m_torrentInfo = result.value_or(BitTorrent::TorrentInfo());
if (!result)
{
RaisedMessageBox::critical(this, tr("Invalid torrent"), tr("Failed to load from URL: %1.\nError: %2")
.arg(downloadResult.url, result.error()));
return;
}
m_torrentGuard = std::make_unique<TorrentFileGuard>();
if (loadTorrentImpl())
open();
else
deleteLater();
}
m_torrentGuard = std::make_unique<TorrentFileGuard>();
if (loadTorrentImpl())
open();
else
deleteLater();
break;
case Net::DownloadStatus::RedirectedToMagnet:
if (loadMagnet(BitTorrent::MagnetUri(result.magnet)))
if (loadMagnet(BitTorrent::MagnetUri(downloadResult.magnet)))
open();
else
deleteLater();
break;
default:
RaisedMessageBox::critical(this, tr("Download Error"),
tr("Cannot download '%1': %2").arg(result.url, result.errorString));
tr("Cannot download '%1': %2").arg(downloadResult.url, downloadResult.errorString));
deleteLater();
}
}