1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-27 15:04:36 +00:00

Unify custom exceptions

This commit is contained in:
Vladimir Golovnev (Glassez) 2021-05-05 16:41:30 +03:00
parent 89cedd411e
commit 9565b695ef
No known key found for this signature in database
GPG Key ID: 52A2C7DEE2DFA6F7
13 changed files with 43 additions and 57 deletions

View File

@ -638,7 +638,7 @@ int Application::exec(const QStringList &params)
catch (const RuntimeError &err) catch (const RuntimeError &err)
{ {
#ifdef DISABLE_GUI #ifdef DISABLE_GUI
fprintf(stderr, "%s", err.what()); fprintf(stderr, "%s", qPrintable(err.message()));
#else #else
QMessageBox msgBox; QMessageBox msgBox;
msgBox.setIcon(QMessageBox::Critical); msgBox.setIcon(QMessageBox::Critical);

View File

@ -498,17 +498,6 @@ QBtCommandLineParameters parseCommandLine(const QStringList &args)
return result; 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) QString wrapText(const QString &text, int initialIndentation = USAGE_TEXT_COLUMN, int wrapAtColumn = WRAP_AT_COLUMN)
{ {
QStringList words = text.split(' '); QStringList words = text.split(' ');

View File

@ -31,11 +31,12 @@
#pragma once #pragma once
#include <optional> #include <optional>
#include <stdexcept>
#include <QString> #include <QString>
#include <QStringList> #include <QStringList>
#include "base/exceptions.h"
class QProcessEnvironment; class QProcessEnvironment;
struct QBtCommandLineParameters struct QBtCommandLineParameters
@ -67,14 +68,10 @@ struct QBtCommandLineParameters
QStringList paramList() const; QStringList paramList() const;
}; };
class CommandLineParameterError : public std::runtime_error class CommandLineParameterError : public RuntimeError
{ {
public: public:
explicit CommandLineParameterError(const QString &messageForUser); using RuntimeError::RuntimeError;
const QString &messageForUser() const;
private:
const QString m_messageForUser;
}; };
QBtCommandLineParameters parseCommandLine(const QStringList &args); QBtCommandLineParameters parseCommandLine(const QStringList &args);

View File

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

View File

@ -265,7 +265,7 @@ Http::Response Tracker::processRequest(const Http::Request &request, const Http:
const lt::entry::dictionary_type bencodedEntry = const lt::entry::dictionary_type bencodedEntry =
{ {
{ANNOUNCE_RESPONSE_FAILURE_REASON, {error.what()}} {ANNOUNCE_RESPONSE_FAILURE_REASON, {error.message().toStdString()}}
}; };
QByteArray reply; QByteArray reply;
lt::bencode(std::back_inserter(reply), bencodedEntry); lt::bencode(std::back_inserter(reply), bencodedEntry);

View File

@ -28,12 +28,12 @@
#include "exceptions.h" #include "exceptions.h"
RuntimeError::RuntimeError(const QString &message) Exception::Exception(const QString &message) noexcept
: std::runtime_error {message.toUtf8().data()} : m_message {message}
{ {
} }
QString RuntimeError::message() const QString Exception::message() const noexcept
{ {
return what(); return m_message;
} }

View File

@ -1,6 +1,6 @@
/* /*
* Bittorrent Client using Qt and libtorrent. * 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 * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -28,12 +28,20 @@
#pragma once #pragma once
#include <stdexcept>
#include <QString> #include <QString>
class RuntimeError : public std::runtime_error class Exception
{ {
public: public:
explicit RuntimeError(const QString &message = {}); explicit Exception(const QString &message = {}) noexcept;
QString message() const; [[nodiscard]] QString message() const noexcept;
private:
QString m_message;
};
class RuntimeError : public Exception
{
public:
using Exception::Exception;
}; };

View File

@ -114,7 +114,7 @@ AutoDownloader::AutoDownloader()
m_fileStorage = new AsyncFileStorage( m_fileStorage = new AsyncFileStorage(
Utils::Fs::expandPathAbs(specialFolderLocation(SpecialFolder::Config) + ConfFolderName)); Utils::Fs::expandPathAbs(specialFolderLocation(SpecialFolder::Config) + ConfFolderName));
if (!m_fileStorage) 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); m_fileStorage->moveToThread(m_ioThread);
connect(m_ioThread, &QThread::finished, m_fileStorage, &AsyncFileStorage::deleteLater); connect(m_ioThread, &QThread::finished, m_fileStorage, &AsyncFileStorage::deleteLater);
@ -527,13 +527,3 @@ void AutoDownloader::timerEvent(QTimerEvent *event)
Q_UNUSED(event); Q_UNUSED(event);
store(); store();
} }
ParsingError::ParsingError(const QString &message)
: std::runtime_error(message.toUtf8().data())
{
}
QString ParsingError::message() const
{
return what();
}

View File

@ -28,8 +28,6 @@
#pragma once #pragma once
#include <stdexcept>
#include <QBasicTimer> #include <QBasicTimer>
#include <QHash> #include <QHash>
#include <QList> #include <QList>
@ -38,6 +36,8 @@
#include <QRegularExpression> #include <QRegularExpression>
#include <QSharedPointer> #include <QSharedPointer>
#include "base/exceptions.h"
class QThread; class QThread;
class QTimer; class QTimer;
@ -53,11 +53,10 @@ namespace RSS
class AutoDownloadRule; class AutoDownloadRule;
class ParsingError : public std::runtime_error class ParsingError : public RuntimeError
{ {
public: public:
explicit ParsingError(const QString &message); using RuntimeError::RuntimeError;
QString message() const;
}; };
class AutoDownloader final : public QObject class AutoDownloader final : public QObject

View File

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

View File

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

View File

@ -29,11 +29,12 @@
#pragma once #pragma once
#include <array> #include <array>
#include <stdexcept>
#include <QDebug> #include <QDebug>
#include <QString> #include <QString>
#include "base/exceptions.h"
namespace Utils namespace Utils
{ {
template <typename T, std::size_t N, std::size_t Mandatory = N> template <typename T, std::size_t N, std::size_t Mandatory = N>
@ -64,7 +65,7 @@ namespace Utils
* @brief Creates version from string in format "x.y.z" * @brief Creates version from string in format "x.y.z"
* *
* @param version Version 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(const QString &version)
: Version {version.split(QLatin1Char('.'))} : Version {version.split(QLatin1Char('.'))}
@ -75,7 +76,7 @@ namespace Utils
* @brief Creates version from byte array in format "x.y.z" * @brief Creates version from byte array in format "x.y.z"
* *
* @param version Version 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 QByteArray &version) Version(const QByteArray &version)
: Version {version.split('.')} : Version {version.split('.')}
@ -150,9 +151,9 @@ namespace Utils
{ {
return Version(s); 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; return defaultVersion;
} }
} }
@ -165,7 +166,9 @@ namespace Utils
{ {
if ((static_cast<std::size_t>(versionParts.size()) > N) if ((static_cast<std::size_t>(versionParts.size()) > N)
|| (static_cast<std::size_t>(versionParts.size()) < Mandatory)) || (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; bool ok = false;
ComponentsArray res {{}}; ComponentsArray res {{}};
@ -173,7 +176,7 @@ namespace Utils
{ {
res[i] = static_cast<T>(versionParts[static_cast<typename StringsList::size_type>(i)].toInt(&ok)); res[i] = static_cast<T>(versionParts[static_cast<typename StringsList::size_type>(i)].toInt(&ok));
if (!ok) if (!ok)
throw std::runtime_error("Can not parse version component"); throw RuntimeError(QLatin1String("Can not parse version component"));
} }
return res; return res;
} }

View File

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