diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 970032733..98599d0a0 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -69,7 +69,6 @@ #include #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 newTorrentPath = exportDir.absoluteFilePath(torrentExportFilename); } - try - { - torrentInfo.saveToFile(newTorrentPath); - } - catch (const RuntimeError &err) + const nonstd::expected 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); } } } diff --git a/src/base/bittorrent/torrentinfo.cpp b/src/base/bittorrent/torrentinfo.cpp index 7a514b427..317fb9726 100644 --- a/src/base/bittorrent/torrentinfo.cpp +++ b/src/base/bittorrent/torrentinfo.cpp @@ -40,7 +40,6 @@ #include #include -#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::loadFromFile(const QString & return load(data); } -void TorrentInfo::saveToFile(const QString &path) const +nonstd::expected 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 const lt::entry torrentEntry = torrentCreator.generate(); const nonstd::expected 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 diff --git a/src/base/bittorrent/torrentinfo.h b/src/base/bittorrent/torrentinfo.h index eb98d878c..a015fa758 100644 --- a/src/base/bittorrent/torrentinfo.h +++ b/src/base/bittorrent/torrentinfo.h @@ -58,7 +58,7 @@ namespace BitTorrent static nonstd::expected load(const QByteArray &data) noexcept; static nonstd::expected loadFromFile(const QString &path) noexcept; - void saveToFile(const QString &path) const; + nonstd::expected saveToFile(const QString &path) const; TorrentInfo &operator=(const TorrentInfo &other); diff --git a/src/gui/addnewtorrentdialog.cpp b/src/gui/addnewtorrentdialog.cpp index 7320e2647..2e3dc01e5 100644 --- a/src/gui/addnewtorrentdialog.cpp +++ b/src/gui/addnewtorrentdialog.cpp @@ -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() if (!path.endsWith(torrentFileExtension, Qt::CaseInsensitive)) path += torrentFileExtension; - try - { - m_torrentInfo.saveToFile(path); - } - catch (const RuntimeError &err) + const nonstd::expected 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())); } }