Don't use output parameters for error handling

This commit is contained in:
Vladimir Golovnev (Glassez)
2021-10-06 21:45:37 +03:00
parent 4d480b8761
commit 41fc0fd084
12 changed files with 116 additions and 165 deletions

View File

@@ -106,7 +106,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 +117,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,20 +142,15 @@ 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