Browse Source

Unify custom exceptions

adaptive-webui-19844
Vladimir Golovnev (Glassez) 4 years ago
parent
commit
9565b695ef
No known key found for this signature in database
GPG Key ID: 52A2C7DEE2DFA6F7
  1. 2
      src/app/application.cpp
  2. 11
      src/app/cmdoptions.cpp
  3. 11
      src/app/cmdoptions.h
  4. 2
      src/app/main.cpp
  5. 2
      src/base/bittorrent/tracker.cpp
  6. 8
      src/base/exceptions.cpp
  7. 18
      src/base/exceptions.h
  8. 12
      src/base/rss/rss_autodownloader.cpp
  9. 9
      src/base/rss/rss_autodownloader.h
  10. 4
      src/base/rss/rss_feed.cpp
  11. 2
      src/base/utils/foreignapps.cpp
  12. 17
      src/base/utils/version.h
  13. 2
      src/gui/programupdater.cpp

2
src/app/application.cpp

@ -638,7 +638,7 @@ int Application::exec(const QStringList &params) @@ -638,7 +638,7 @@ int Application::exec(const QStringList &params)
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);

11
src/app/cmdoptions.cpp

@ -498,17 +498,6 @@ QBtCommandLineParameters parseCommandLine(const QStringList &args) @@ -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(' ');

11
src/app/cmdoptions.h

@ -31,11 +31,12 @@ @@ -31,11 +31,12 @@
#pragma once
#include <optional>
#include <stdexcept>
#include <QString>
#include <QStringList>
#include "base/exceptions.h"
class QProcessEnvironment;
struct QBtCommandLineParameters
@ -67,14 +68,10 @@ 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);

2
src/app/main.cpp

@ -311,7 +311,7 @@ int main(int argc, char *argv[]) @@ -311,7 +311,7 @@ int main(int argc, char *argv[])
}
catch (const CommandLineParameterError &er)
{
displayBadArgMessage(er.messageForUser());
displayBadArgMessage(er.message());
return EXIT_FAILURE;
}
}

2
src/base/bittorrent/tracker.cpp

@ -265,7 +265,7 @@ Http::Response Tracker::processRequest(const Http::Request &request, const Http: @@ -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);

8
src/base/exceptions.cpp

@ -28,12 +28,12 @@ @@ -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;
}

18
src/base/exceptions.h

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2018 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2018, 2021 Vladimir Golovnev <glassez@yandex.ru>
*
* 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 @@ @@ -28,12 +28,20 @@
#pragma once
#include <stdexcept>
#include <QString>
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;
};

12
src/base/rss/rss_autodownloader.cpp

@ -114,7 +114,7 @@ AutoDownloader::AutoDownloader() @@ -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) @@ -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();
}

9
src/base/rss/rss_autodownloader.h

@ -28,8 +28,6 @@ @@ -28,8 +28,6 @@
#pragma once
#include <stdexcept>
#include <QBasicTimer>
#include <QHash>
#include <QList>
@ -38,6 +36,8 @@ @@ -38,6 +36,8 @@
#include <QRegularExpression>
#include <QSharedPointer>
#include "base/exceptions.h"
class QThread;
class QTimer;
@ -53,11 +53,10 @@ namespace RSS @@ -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

4
src/base/rss/rss_feed.cpp

@ -313,7 +313,7 @@ void Feed::loadArticles(const QByteArray &data) @@ -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() @@ -335,7 +335,7 @@ void Feed::loadArticlesLegacy()
if (!addArticle(article))
delete article;
}
catch (const std::runtime_error&) {}
catch (const RuntimeError &) {}
}
}

2
src/base/utils/foreignapps.cpp

@ -76,7 +76,7 @@ namespace @@ -76,7 +76,7 @@ namespace
{
info = {exeName, versionStr.left(idx)};
}
catch (const std::runtime_error &)
catch (const RuntimeError &)
{
return false;
}

17
src/base/utils/version.h

@ -29,11 +29,12 @@ @@ -29,11 +29,12 @@
#pragma once
#include <array>
#include <stdexcept>
#include <QDebug>
#include <QString>
#include "base/exceptions.h"
namespace Utils
{
template <typename T, std::size_t N, std::size_t Mandatory = N>
@ -64,7 +65,7 @@ namespace Utils @@ -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 @@ -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 @@ -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 @@ -165,7 +166,9 @@ namespace Utils
{
if ((static_cast<std::size_t>(versionParts.size()) > N)
|| (static_cast<std::size_t>(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 @@ -173,7 +176,7 @@ namespace Utils
{
res[i] = static_cast<T>(versionParts[static_cast<typename StringsList::size_type>(i)].toInt(&ok));
if (!ok)
throw std::runtime_error("Can not parse version component");
throw RuntimeError(QLatin1String("Can not parse version component"));
}
return res;
}

2
src/gui/programupdater.cpp

@ -66,7 +66,7 @@ namespace @@ -66,7 +66,7 @@ namespace
}
return (newVersion > currentVersion);
}
catch (const std::runtime_error &)
catch (const RuntimeError &)
{
return false;
}

Loading…
Cancel
Save