Skip to content

Commit

Permalink
unified almost all throw exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
silverqx committed Jan 17, 2022
1 parent 245fd10 commit 71d22f2
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 29 deletions.
4 changes: 4 additions & 0 deletions NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,12 @@ RegExs:

- const data members:
(?<![\(\)])(const) +.* +\bm_.*\b( +.*)?;$
(?<![\(\)])(const) +.* +\bm_.*\b +=
(?<![\(\)])(const) +.* +\bm_.*\b +{
- const data member references:
(?<![\(\)])(const) +.* +&\bm_.*\b( +.*)?;$
- all exceptions:
throw (.*::)?\w+(E|_error)


Powershell commands:
Expand Down
13 changes: 6 additions & 7 deletions include/orm/tiny/concerns/hasrelationships.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ TINY_SYSTEM_HEADER

#include <range/v3/algorithm/contains.hpp>

#include "orm/exceptions/invalidargumenterror.hpp"
#include "orm/exceptions/invalidtemplateargumenterror.hpp"
#include "orm/tiny/concerns/hasrelationstore.hpp"
#include "orm/tiny/exceptions/relationnotfounderror.hpp"
#include "orm/tiny/exceptions/relationnotloadederror.hpp"
Expand Down Expand Up @@ -931,23 +931,22 @@ namespace Concerns
{
if constexpr (std::is_same_v<Result, std::optional<Related>>) {
if (!std::holds_alternative<Result>(relationVariant))
throw Orm::Exceptions::RuntimeError(
throw Orm::Exceptions::InvalidTemplateArgumentError(
QStringLiteral(
"The relation '%1' is many type relation, use "
"%2<%3>() method overload without an 'Orm::One' tag.")
.arg(relation, source,
Orm::Utils::Type::classPureBasename<Related>()));
} else if constexpr (std::is_same_v<Result, QVector<Related>>) {
if (!std::holds_alternative<Result>(relationVariant))
throw Orm::Exceptions::RuntimeError(
throw Orm::Exceptions::InvalidTemplateArgumentError(
QStringLiteral(
"The relation '%1' is one type relation, use "
"%2<%3, Orm::One>() method overload "
"with an 'Orm::One' tag.")
"%2<%3, Orm::One>() method overload with an 'Orm::One' tag.")
.arg(relation, source,
Orm::Utils::Type::classPureBasename<Related>()));
} else
throw Orm::Exceptions::InvalidArgumentError(
throw Orm::Exceptions::InvalidTemplateArgumentError(
"Unexpected 'Result' template argument.");
}

Expand Down Expand Up @@ -1126,7 +1125,7 @@ namespace Concerns
)
relatedModel->touchOwners();
} else
throw Orm::Exceptions::RuntimeError(
throw Orm::Exceptions::InvalidTemplateArgumentError(
"Bad relation type passed to the Model::touchOwnersVisited().");
}

Expand Down
2 changes: 1 addition & 1 deletion include/orm/tiny/tinybuilder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ namespace Orm::Tiny
const auto isSelectConstraint = relation.name.contains(COLON);

if (isSelectConstraint && relation.constraints)
throw Orm::Exceptions::RuntimeError(
throw Orm::Exceptions::InvalidArgumentError(
"Passing both 'Select constraint' and 'Lambda expression "
"constraint' to the Model::with() method is not allowed, use "
"only one of them.");
Expand Down
5 changes: 4 additions & 1 deletion src/orm/basegrammar.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "orm/basegrammar.hpp"

#include "orm/exceptions/runtimeerror.hpp"
#include "orm/utils/type.hpp"

TINYORM_BEGIN_COMMON_NAMESPACE

Expand Down Expand Up @@ -62,7 +63,9 @@ QString BaseGrammar::wrapTable(const QString &table) const
QString BaseGrammar::wrapTable(const FromClause &table) const
{
if (std::holds_alternative<std::monostate>(table))
throw Exceptions::RuntimeError("std::monostate in wrapTable().");
throw Exceptions::RuntimeError(
QStringLiteral("Unexpected std::monostate value in %1().")
.arg(__tiny_func__));

if (std::holds_alternative<Expression>(table))
return getValue(std::get<Expression>(table)).value<QString>();
Expand Down
2 changes: 1 addition & 1 deletion src/orm/connectors/connector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Connector::createQSqlDatabaseConnection(const QString &name, const QVariantHash

if (!db.open())
throw Exceptions::SqlError(
QStringLiteral("Open databse connection in %1() failed.")
QStringLiteral("Failed to open database connection in %1().")
.arg(__tiny_func__),
db.lastError());

Expand Down
12 changes: 7 additions & 5 deletions src/orm/connectors/mysqlconnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void MySqlConnector::parseConfigOptions(QVariantHash &options) const
{
// This connection options are banned
static const QVariantHash banned {
// We have our reconnector
// We have our own reconnector
{QStringLiteral("MYSQL_OPT_RECONNECT"), 1},
};

Expand All @@ -72,11 +72,13 @@ void MySqlConnector::parseConfigOptions(QVariantHash &options) const
const auto &key = itOption.key();
const auto &value = itOption.value();

// BUG rewrite silverqx
if (options.contains(key) && options[key] == value)
throw std::domain_error(
"The connection option '" + value.value<QString>().toStdString() +
"' is not allowed in the TinyORM, TinyORM uses its own "
"reconnector.");
throw Exceptions::RuntimeError(
QStringLiteral(
"The connection option '%1' is not allowed in the TinyORM, "
"TinyORM uses its own reconnector.")
.arg(key));

++itOption;
}
Expand Down
7 changes: 5 additions & 2 deletions src/orm/databaseconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ QSqlQuery DatabaseConnection::unprepared(const QString &queryString)
include the bindings with SQL, which will make this exception a lot
more helpful to the developer instead of just the database's errors. */
throw Exceptions::QueryError(
QStringLiteral("Statement in %1() failed.").arg(functionName),
QStringLiteral("Unprepared statement in %1() failed.")
.arg(functionName),
query);
});
}
Expand Down Expand Up @@ -360,7 +361,9 @@ bool DatabaseConnection::pingDatabase()
void DatabaseConnection::reconnect() const
{
if (!m_reconnector)
throw std::runtime_error("Lost connection and no reconnector available.");
throw Exceptions::RuntimeError(
QStringLiteral("Lost connection and no reconnector available in %1.")
.arg(__tiny_func__));

std::invoke(m_reconnector, *this);
}
Expand Down
6 changes: 4 additions & 2 deletions src/orm/databasemanager.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "orm/databasemanager.hpp"

#include "orm/concerns/hasconnectionresolver.hpp"
#include "orm/exceptions/invalidargumenterror.hpp"

TINYORM_BEGIN_COMMON_NAMESPACE

Expand Down Expand Up @@ -213,8 +214,9 @@ DatabaseManager &
DatabaseManager::addConnection(const QVariantHash &config, const QString &name)
{
if ((*m_configuration).contains(name))
throw Exceptions::RuntimeError(
QStringLiteral("The database connection '%1' already exists.").arg(name));
throw Exceptions::InvalidArgumentError(
QStringLiteral("The database connection '%1' already exists.")
.arg(name));

(*m_configuration).insert(name, config);

Expand Down
2 changes: 1 addition & 1 deletion src/orm/query/querybuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ Builder &Builder::orderBy(const Column &column, const QString &direction)
const auto &directionLower = direction.toLower();

if (directionLower != ASC && directionLower != DESC)
throw Exceptions::RuntimeError(
throw Exceptions::InvalidArgumentError(
R"T(Order direction must be "asc" or "desc", case is not important.)T");

m_orders.append({column, directionLower});
Expand Down
9 changes: 4 additions & 5 deletions src/orm/support/configurationoptionsparser.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "orm/support/configurationoptionsparser.hpp"

#include <stdexcept>

#include "orm/connectors/connector.hpp"
#include "orm/constants.hpp"
#include "orm/exceptions/runtimeerror.hpp"

using Orm::Constants::EQ_C;
using Orm::Constants::options_;
Expand Down Expand Up @@ -53,9 +52,9 @@ ConfigurationOptionsParser::validateConfigOptions(const QVariant &options) const
#endif
&& !options.canConvert<QVariantHash>()
)
throw std::domain_error(
"The unsupported 'options' type in the connection configuration "
"has to be QString or QVariantHash.");
throw Exceptions::RuntimeError(
"Passed unsupported 'options' type in the connection configuration, "
"it has to be the QString or QVariantHash type.");
}

QVariantHash
Expand Down
6 changes: 2 additions & 4 deletions tests/TinyUtils/src/databases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "orm/constants.hpp"
#include "orm/db.hpp"
#include "orm/exceptions/logicerror.hpp"
#include "orm/exceptions/runtimeerror.hpp"

using Orm::Constants::database_;
Expand Down Expand Up @@ -35,7 +34,6 @@ using Orm::Constants::UTF8MB4;

using Orm::DB;

using Orm::Exceptions::LogicError;
using Orm::Exceptions::RuntimeError;

namespace TestUtils
Expand Down Expand Up @@ -225,8 +223,8 @@ void Databases::throwIfConnectionsInitialized()
static bool initialized = false;

if (initialized)
throw LogicError("Databases::createConnections/createConnection methods "
"can be called only once.");
throw RuntimeError("Databases::createConnections/createConnection methods "
"can be called only once.");

initialized = true;
}
Expand Down

0 comments on commit 71d22f2

Please sign in to comment.