Browse Source

Don't throw exception in TorrentInfo::saveToFile()

adaptive-webui-19844
Vladimir Golovnev (Glassez) 3 years ago
parent
commit
78459fcb31
No known key found for this signature in database
GPG Key ID: 52A2C7DEE2DFA6F7
  1. 10
      src/base/bittorrent/session.cpp
  2. 11
      src/base/bittorrent/torrentinfo.cpp
  3. 2
      src/base/bittorrent/torrentinfo.h
  4. 10
      src/gui/addnewtorrentdialog.cpp

10
src/base/bittorrent/session.cpp

@ -69,7 +69,6 @@ @@ -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"
@ -2317,14 +2316,11 @@ void Session::exportTorrentFile(const TorrentInfo &torrentInfo, const QString &f @@ -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);
}
}
}

11
src/base/bittorrent/torrentinfo.cpp

@ -40,7 +40,6 @@ @@ -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"
@ -153,10 +152,10 @@ nonstd::expected<TorrentInfo, QString> TorrentInfo::loadFromFile(const QString & @@ -153,10 +152,10 @@ nonstd::expected<TorrentInfo, QString> TorrentInfo::loadFromFile(const QString &
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
{
@ -164,12 +163,14 @@ void TorrentInfo::saveToFile(const QString &path) const @@ -164,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

2
src/base/bittorrent/torrentinfo.h

@ -58,7 +58,7 @@ namespace BitTorrent @@ -58,7 +58,7 @@ namespace BitTorrent
static nonstd::expected<TorrentInfo, QString> load(const QByteArray &data) noexcept;
static nonstd::expected<TorrentInfo, QString> loadFromFile(const QString &path) noexcept;
void saveToFile(const QString &path) const;
nonstd::expected<void, QString> saveToFile(const QString &path) const;
TorrentInfo &operator=(const TorrentInfo &other);

10
src/gui/addnewtorrentdialog.cpp

@ -43,7 +43,6 @@ @@ -43,7 +43,6 @@
#include "base/bittorrent/magneturi.h"
#include "base/bittorrent/session.h"
#include "base/bittorrent/torrent.h"
#include "base/exceptions.h"
#include "base/global.h"
#include "base/net/downloadmanager.h"
#include "base/settingsstorage.h"
@ -473,14 +472,11 @@ void AddNewTorrentDialog::saveTorrentFile() @@ -473,14 +472,11 @@ void AddNewTorrentDialog::saveTorrentFile()
if (!path.endsWith(torrentFileExtension, Qt::CaseInsensitive))
path += torrentFileExtension;
try
{
m_torrentInfo.saveToFile(path);
}
catch (const RuntimeError &err)
const nonstd::expected<void, QString> result = m_torrentInfo.saveToFile(path);
if (!result)
{
QMessageBox::critical(this, tr("I/O Error")
, tr("Couldn't export torrent metadata file '%1'. Reason: %2.").arg(path, err.message()));
, tr("Couldn't export torrent metadata file '%1'. Reason: %2.").arg(path, result.error()));
}
}

Loading…
Cancel
Save