Merge pull request #15536 from glassez/expected

Don't use output parameters for error handling
This commit is contained in:
Vladimir Golovnev
2021-10-10 15:04:55 +03:00
committed by GitHub
12 changed files with 129 additions and 185 deletions

View File

@@ -69,7 +69,6 @@
#include <QUuid>
#include "base/algorithm.h"
#include "base/exceptions.h"
#include "base/global.h"
#include "base/logger.h"
#include "base/net/downloadmanager.h"
@@ -1698,7 +1697,7 @@ void Session::handleDownloadFinished(const Net::DownloadResult &result)
{
case Net::DownloadStatus::Success:
emit downloadFromUrlFinished(result.url);
addTorrent(TorrentInfo::load(result.data), m_downloadedTorrents.take(result.url));
addTorrent(TorrentInfo::load(result.data).value_or(TorrentInfo()), m_downloadedTorrents.take(result.url));
break;
case Net::DownloadStatus::RedirectedToMagnet:
emit downloadFromUrlFinished(result.url);
@@ -2028,7 +2027,7 @@ bool Session::addTorrent(const QString &source, const AddTorrentParams &params)
return addTorrent(magnetUri, params);
TorrentFileGuard guard {source};
if (addTorrent(TorrentInfo::loadFromFile(source), params))
if (addTorrent(TorrentInfo::loadFromFile(source).value_or(TorrentInfo()), params))
{
guard.markAsAddedToSession();
return true;
@@ -2317,14 +2316,11 @@ void Session::exportTorrentFile(const TorrentInfo &torrentInfo, const QString &f
newTorrentPath = exportDir.absoluteFilePath(torrentExportFilename);
}
try
{
torrentInfo.saveToFile(newTorrentPath);
}
catch (const RuntimeError &err)
const nonstd::expected<void, QString> result = torrentInfo.saveToFile(newTorrentPath);
if (!result)
{
LogMsg(tr("Couldn't export torrent metadata file '%1'. Reason: %2.")
.arg(newTorrentPath, err.message()), Log::WARNING);
.arg(newTorrentPath, result.error()), Log::WARNING);
}
}
}
@@ -3938,8 +3934,7 @@ void Session::handleTorrentFinished(TorrentImpl *const torrent)
qDebug("Found possible recursive torrent download.");
const QString torrentFullpath = torrent->savePath(true) + '/' + torrentRelpath;
qDebug("Full subtorrent path is %s", qUtf8Printable(torrentFullpath));
TorrentInfo torrentInfo = TorrentInfo::loadFromFile(torrentFullpath);
if (torrentInfo.isValid())
if (TorrentInfo::loadFromFile(torrentFullpath))
{
qDebug("emitting recursiveTorrentDownloadPossible()");
emit recursiveTorrentDownloadPossible(torrent);
@@ -4167,7 +4162,7 @@ void Session::recursiveTorrentDownload(const TorrentID &id)
AddTorrentParams params;
// Passing the save path along to the sub torrent file
params.savePath = torrent->savePath();
addTorrent(TorrentInfo::loadFromFile(torrentFullpath), params);
addTorrent(TorrentInfo::loadFromFile(torrentFullpath).value_or(TorrentInfo()), params);
}
}
}

View File

@@ -40,7 +40,6 @@
#include <QUrl>
#include <QVector>
#include "base/exceptions.h"
#include "base/global.h"
#include "base/utils/fs.h"
#include "base/utils/io.h"
@@ -106,7 +105,7 @@ TorrentInfo &TorrentInfo::operator=(const TorrentInfo &other)
return *this;
}
TorrentInfo TorrentInfo::load(const QByteArray &data, QString *error) noexcept
nonstd::expected<TorrentInfo, QString> TorrentInfo::load(const QByteArray &data) noexcept
{
// 2-step construction to overcome default limits of `depth_limit` & `token_limit` which are
// used in `torrent_info()` constructor
@@ -117,42 +116,23 @@ TorrentInfo TorrentInfo::load(const QByteArray &data, QString *error) noexcept
const lt::bdecode_node node = lt::bdecode(data, ec
, nullptr, depthLimit, tokenLimit);
if (ec)
{
if (error)
*error = QString::fromStdString(ec.message());
return TorrentInfo();
}
return nonstd::make_unexpected(QString::fromStdString(ec.message()));
TorrentInfo info {std::shared_ptr<lt::torrent_info>(new lt::torrent_info(node, ec))};
if (ec)
{
if (error)
*error = QString::fromStdString(ec.message());
return TorrentInfo();
}
return nonstd::make_unexpected(QString::fromStdString(ec.message()));
return info;
}
TorrentInfo TorrentInfo::loadFromFile(const QString &path, QString *error) noexcept
nonstd::expected<TorrentInfo, QString> TorrentInfo::loadFromFile(const QString &path) noexcept
{
if (error)
error->clear();
QFile file {path};
if (!file.open(QIODevice::ReadOnly))
{
if (error)
*error = file.errorString();
return TorrentInfo();
}
return nonstd::make_unexpected(file.errorString());
if (file.size() > MAX_TORRENT_SIZE)
{
if (error)
*error = tr("File size exceeds max limit %1").arg(Utils::Misc::friendlyUnit(MAX_TORRENT_SIZE));
return TorrentInfo();
}
return nonstd::make_unexpected(tr("File size exceeds max limit %1").arg(Utils::Misc::friendlyUnit(MAX_TORRENT_SIZE)));
QByteArray data;
try
@@ -161,26 +141,21 @@ TorrentInfo TorrentInfo::loadFromFile(const QString &path, QString *error) noexc
}
catch (const std::bad_alloc &e)
{
if (error)
*error = tr("Torrent file read error: %1").arg(e.what());
return TorrentInfo();
return nonstd::make_unexpected(tr("Torrent file read error: %1").arg(e.what()));
}
if (data.size() != file.size())
{
if (error)
*error = tr("Torrent file read error: size mismatch");
return TorrentInfo();
}
return nonstd::make_unexpected(tr("Torrent file read error: size mismatch"));
file.close();
return load(data, error);
return load(data);
}
void TorrentInfo::saveToFile(const QString &path) const
nonstd::expected<void, QString> TorrentInfo::saveToFile(const QString &path) const
{
if (!isValid())
throw RuntimeError {tr("Invalid metadata")};
return nonstd::make_unexpected(tr("Invalid metadata"));
try
{
@@ -188,12 +163,14 @@ void TorrentInfo::saveToFile(const QString &path) const
const lt::entry torrentEntry = torrentCreator.generate();
const nonstd::expected<void, QString> result = Utils::IO::saveToFile(path, torrentEntry);
if (!result)
throw RuntimeError(result.error());
return result.get_unexpected();
}
catch (const lt::system_error &err)
{
throw RuntimeError(QString::fromLocal8Bit(err.what()));
return nonstd::make_unexpected(QString::fromLocal8Bit(err.what()));
}
return {};
}
bool TorrentInfo::isValid() const

View File

@@ -33,6 +33,7 @@
#include <QCoreApplication>
#include <QtContainerFwd>
#include "base/3rdparty/expected.hpp"
#include "base/indexrange.h"
#include "abstractfilestorage.h"
#include "torrentcontentlayout.h"
@@ -55,9 +56,9 @@ namespace BitTorrent
explicit TorrentInfo(std::shared_ptr<const lt::torrent_info> nativeInfo = {});
TorrentInfo(const TorrentInfo &other);
static TorrentInfo load(const QByteArray &data, QString *error = nullptr) noexcept;
static TorrentInfo loadFromFile(const QString &path, QString *error = nullptr) noexcept;
void saveToFile(const QString &path) const;
static nonstd::expected<TorrentInfo, QString> load(const QByteArray &data) noexcept;
static nonstd::expected<TorrentInfo, QString> loadFromFile(const QString &path) noexcept;
nonstd::expected<void, QString> saveToFile(const QString &path) const;
TorrentInfo &operator=(const TorrentInfo &other);