diff --git a/src/app/application.cpp b/src/app/application.cpp index e03b0cf77..7bf6ee0c4 100644 --- a/src/app/application.cpp +++ b/src/app/application.cpp @@ -638,7 +638,7 @@ int Application::exec(const QStringList ¶ms) catch (const RuntimeError &err) { #ifdef DISABLE_GUI - fprintf(stderr, "%s", err.what()); + fprintf(stderr, "%s", qPrintable(err.message())); #else QMessageBox msgBox; msgBox.setIcon(QMessageBox::Critical); diff --git a/src/app/cmdoptions.cpp b/src/app/cmdoptions.cpp index 6535e1702..15cb4a85a 100644 --- a/src/app/cmdoptions.cpp +++ b/src/app/cmdoptions.cpp @@ -498,17 +498,6 @@ QBtCommandLineParameters parseCommandLine(const QStringList &args) return result; } -CommandLineParameterError::CommandLineParameterError(const QString &messageForUser) - : std::runtime_error(messageForUser.toLocal8Bit().data()) - , m_messageForUser(messageForUser) -{ -} - -const QString &CommandLineParameterError::messageForUser() const -{ - return m_messageForUser; -} - QString wrapText(const QString &text, int initialIndentation = USAGE_TEXT_COLUMN, int wrapAtColumn = WRAP_AT_COLUMN) { QStringList words = text.split(' '); diff --git a/src/app/cmdoptions.h b/src/app/cmdoptions.h index 2f167b99d..46d84b66e 100644 --- a/src/app/cmdoptions.h +++ b/src/app/cmdoptions.h @@ -31,11 +31,12 @@ #pragma once #include -#include #include #include +#include "base/exceptions.h" + class QProcessEnvironment; struct QBtCommandLineParameters @@ -67,14 +68,10 @@ struct QBtCommandLineParameters QStringList paramList() const; }; -class CommandLineParameterError : public std::runtime_error +class CommandLineParameterError : public RuntimeError { public: - explicit CommandLineParameterError(const QString &messageForUser); - const QString &messageForUser() const; - -private: - const QString m_messageForUser; + using RuntimeError::RuntimeError; }; QBtCommandLineParameters parseCommandLine(const QStringList &args); diff --git a/src/app/main.cpp b/src/app/main.cpp index f3c20339e..11b37e2e1 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -311,7 +311,7 @@ int main(int argc, char *argv[]) } catch (const CommandLineParameterError &er) { - displayBadArgMessage(er.messageForUser()); + displayBadArgMessage(er.message()); return EXIT_FAILURE; } } diff --git a/src/base/bittorrent/tracker.cpp b/src/base/bittorrent/tracker.cpp index 372242d49..ca454b267 100644 --- a/src/base/bittorrent/tracker.cpp +++ b/src/base/bittorrent/tracker.cpp @@ -265,7 +265,7 @@ Http::Response Tracker::processRequest(const Http::Request &request, const Http: const lt::entry::dictionary_type bencodedEntry = { - {ANNOUNCE_RESPONSE_FAILURE_REASON, {error.what()}} + {ANNOUNCE_RESPONSE_FAILURE_REASON, {error.message().toStdString()}} }; QByteArray reply; lt::bencode(std::back_inserter(reply), bencodedEntry); diff --git a/src/base/exceptions.cpp b/src/base/exceptions.cpp index 27527ddbb..66e291fa7 100644 --- a/src/base/exceptions.cpp +++ b/src/base/exceptions.cpp @@ -28,12 +28,12 @@ #include "exceptions.h" -RuntimeError::RuntimeError(const QString &message) - : std::runtime_error {message.toUtf8().data()} +Exception::Exception(const QString &message) noexcept + : m_message {message} { } -QString RuntimeError::message() const +QString Exception::message() const noexcept { - return what(); + return m_message; } diff --git a/src/base/exceptions.h b/src/base/exceptions.h index 4b3ef77a4..7d32739ca 100644 --- a/src/base/exceptions.h +++ b/src/base/exceptions.h @@ -1,6 +1,6 @@ /* * Bittorrent Client using Qt and libtorrent. - * Copyright (C) 2018 Vladimir Golovnev + * Copyright (C) 2018, 2021 Vladimir Golovnev * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -28,12 +28,20 @@ #pragma once -#include #include -class RuntimeError : public std::runtime_error +class Exception { public: - explicit RuntimeError(const QString &message = {}); - QString message() const; + explicit Exception(const QString &message = {}) noexcept; + [[nodiscard]] QString message() const noexcept; + +private: + QString m_message; +}; + +class RuntimeError : public Exception +{ +public: + using Exception::Exception; }; diff --git a/src/base/rss/rss_autodownloader.cpp b/src/base/rss/rss_autodownloader.cpp index 2d8c9b07d..4fa441520 100644 --- a/src/base/rss/rss_autodownloader.cpp +++ b/src/base/rss/rss_autodownloader.cpp @@ -114,7 +114,7 @@ AutoDownloader::AutoDownloader() m_fileStorage = new AsyncFileStorage( Utils::Fs::expandPathAbs(specialFolderLocation(SpecialFolder::Config) + ConfFolderName)); if (!m_fileStorage) - throw std::runtime_error("Directory for RSS AutoDownloader data is unavailable."); + throw RuntimeError(tr("Directory for RSS AutoDownloader data is unavailable.")); m_fileStorage->moveToThread(m_ioThread); connect(m_ioThread, &QThread::finished, m_fileStorage, &AsyncFileStorage::deleteLater); @@ -527,13 +527,3 @@ void AutoDownloader::timerEvent(QTimerEvent *event) Q_UNUSED(event); store(); } - -ParsingError::ParsingError(const QString &message) - : std::runtime_error(message.toUtf8().data()) -{ -} - -QString ParsingError::message() const -{ - return what(); -} diff --git a/src/base/rss/rss_autodownloader.h b/src/base/rss/rss_autodownloader.h index 16f6ef48e..a55b3e380 100644 --- a/src/base/rss/rss_autodownloader.h +++ b/src/base/rss/rss_autodownloader.h @@ -28,8 +28,6 @@ #pragma once -#include - #include #include #include @@ -38,6 +36,8 @@ #include #include +#include "base/exceptions.h" + class QThread; class QTimer; @@ -53,11 +53,10 @@ namespace RSS class AutoDownloadRule; - class ParsingError : public std::runtime_error + class ParsingError : public RuntimeError { public: - explicit ParsingError(const QString &message); - QString message() const; + using RuntimeError::RuntimeError; }; class AutoDownloader final : public QObject diff --git a/src/base/rss/rss_feed.cpp b/src/base/rss/rss_feed.cpp index a22186037..b38fa5b92 100644 --- a/src/base/rss/rss_feed.cpp +++ b/src/base/rss/rss_feed.cpp @@ -313,7 +313,7 @@ void Feed::loadArticles(const QByteArray &data) if (!addArticle(article)) delete article; } - catch (const std::runtime_error&) {} + catch (const RuntimeError &) {} } } @@ -335,7 +335,7 @@ void Feed::loadArticlesLegacy() if (!addArticle(article)) delete article; } - catch (const std::runtime_error&) {} + catch (const RuntimeError &) {} } } diff --git a/src/base/utils/foreignapps.cpp b/src/base/utils/foreignapps.cpp index 2b6987ae0..c517d0942 100644 --- a/src/base/utils/foreignapps.cpp +++ b/src/base/utils/foreignapps.cpp @@ -76,7 +76,7 @@ namespace { info = {exeName, versionStr.left(idx)}; } - catch (const std::runtime_error &) + catch (const RuntimeError &) { return false; } diff --git a/src/base/utils/version.h b/src/base/utils/version.h index 51bbb92b0..a03ea9155 100644 --- a/src/base/utils/version.h +++ b/src/base/utils/version.h @@ -29,11 +29,12 @@ #pragma once #include -#include #include #include +#include "base/exceptions.h" + namespace Utils { template @@ -64,7 +65,7 @@ namespace Utils * @brief Creates version from string in format "x.y.z" * * @param version Version string in format "x.y.z" - * @throws std::runtime_error if parsing fails + * @throws RuntimeError if parsing fails */ Version(const QString &version) : Version {version.split(QLatin1Char('.'))} @@ -75,7 +76,7 @@ namespace Utils * @brief Creates version from byte array in format "x.y.z" * * @param version Version string in format "x.y.z" - * @throws std::runtime_error if parsing fails + * @throws RuntimeError if parsing fails */ Version(const QByteArray &version) : Version {version.split('.')} @@ -150,9 +151,9 @@ namespace Utils { return Version(s); } - catch (const std::runtime_error &er) + catch (const RuntimeError &er) { - qDebug() << "Error parsing version:" << er.what(); + qDebug() << "Error parsing version:" << er.message(); return defaultVersion; } } @@ -165,7 +166,9 @@ namespace Utils { if ((static_cast(versionParts.size()) > N) || (static_cast(versionParts.size()) < Mandatory)) - throw std::runtime_error("Incorrect number of version components"); + { + throw RuntimeError(QLatin1String("Incorrect number of version components")); + } bool ok = false; ComponentsArray res {{}}; @@ -173,7 +176,7 @@ namespace Utils { res[i] = static_cast(versionParts[static_cast(i)].toInt(&ok)); if (!ok) - throw std::runtime_error("Can not parse version component"); + throw RuntimeError(QLatin1String("Can not parse version component")); } return res; } diff --git a/src/gui/programupdater.cpp b/src/gui/programupdater.cpp index c72327822..317509ed9 100644 --- a/src/gui/programupdater.cpp +++ b/src/gui/programupdater.cpp @@ -66,7 +66,7 @@ namespace } return (newVersion > currentVersion); } - catch (const std::runtime_error &) + catch (const RuntimeError &) { return false; }