enhanced exception for sole()

This commit is contained in:
silverqx
2022-11-08 09:00:26 +01:00
parent b8f10e2347
commit 97b39ce4e9
3 changed files with 17 additions and 5 deletions
@@ -17,22 +17,29 @@ namespace Orm::Exceptions
{
public:
/*! Constructor. */
inline explicit MultipleRecordsFoundError(int count);
inline explicit MultipleRecordsFoundError(int count, QString &&functionName);
/*! Get the number of records found. */
inline int count() const noexcept;
/*! Get a function name where the exception occured. */
inline const QString &functionName() const noexcept;
protected:
/*! The number of records found. */
int m_count;
/*! Function name where the exception occured. */
QString m_functionName;
};
/* public */
MultipleRecordsFoundError::MultipleRecordsFoundError(const int count)
: RuntimeError(QStringLiteral("%1 records were found.").arg(count)
MultipleRecordsFoundError::MultipleRecordsFoundError(const int count,
QString &&functionName)
: RuntimeError(QStringLiteral("%1 records were found in %2().")
.arg(count).arg(functionName)
.toUtf8().constData())
, m_count(count)
, m_functionName(std::move(functionName))
{}
int MultipleRecordsFoundError::count() const noexcept
@@ -40,6 +47,11 @@ namespace Orm::Exceptions
return m_count;
}
const QString &MultipleRecordsFoundError::functionName() const noexcept
{
return m_functionName;
}
} // namespace Orm::Exceptions
TINYORM_END_COMMON_NAMESPACE
+1 -1
View File
@@ -287,7 +287,7 @@ namespace Concerns
QStringLiteral("No records found in %1().").arg(__tiny_func__));
if (count > 1)
throw Orm::Exceptions::MultipleRecordsFoundError(count);
throw Orm::Exceptions::MultipleRecordsFoundError(count, __tiny_func__);
return std::move(models.first());
}
+1 -1
View File
@@ -185,7 +185,7 @@ SqlQuery BuildsQueries::sole(const QVector<Column> &columns)
QStringLiteral("No records found in %1().").arg(__tiny_func__));
if (count > 1)
throw Exceptions::MultipleRecordsFoundError(count);
throw Exceptions::MultipleRecordsFoundError(count, __tiny_func__);
query.first();