Sqlite: Cleanup exceptions

There are now two base exceptions. Exception and ExceptionWithMessage.
We very often don't use the extra message. So don't pay for what you
don't use. Otherwise it is replacing code with using statements.

Change-Id: I213cf1f02718f6d3b2aa0e2a64626c9fc5c7c917
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Marco Bubke
2021-12-07 16:18:02 +01:00
parent 596ac61872
commit 4e69b0a689
4 changed files with 87 additions and 182 deletions

View File

@@ -297,7 +297,7 @@ void DatabaseBackend::checkDatabaseCouldBeOpened(int resultCode)
return; return;
default: default:
closeWithoutException(); closeWithoutException();
throw Exception( throw UnknowError(
"SqliteDatabaseBackend::SqliteDatabaseBackend: database cannot be opened:", "SqliteDatabaseBackend::SqliteDatabaseBackend: database cannot be opened:",
sqlite3_errmsg(sqliteDatabaseHandle())); sqlite3_errmsg(sqliteDatabaseHandle()));
} }
@@ -473,7 +473,7 @@ void DatabaseBackend::throwExceptionStatic(const char *whatHasHappens)
void DatabaseBackend::throwException(const char *whatHasHappens) const void DatabaseBackend::throwException(const char *whatHasHappens) const
{ {
if (m_databaseHandle) if (m_databaseHandle)
throw Exception(whatHasHappens, sqlite3_errmsg(m_databaseHandle)); throw ExceptionWithMessage(whatHasHappens, sqlite3_errmsg(m_databaseHandle));
else else
throw Exception(whatHasHappens); throw Exception(whatHasHappens);
} }

View File

@@ -31,12 +31,9 @@
namespace Sqlite { namespace Sqlite {
void Exception::printWarning() const void ExceptionWithMessage::printWarning() const
{ {
if (!m_sqliteErrorMessage.isEmpty()) qWarning() << what() << m_sqliteErrorMessage;
qWarning() << m_whatErrorHasHappen << m_sqliteErrorMessage;
else
qWarning() << m_whatErrorHasHappen;
} }
} // namespace Sqlite } // namespace Sqlite

View File

@@ -37,385 +37,293 @@ namespace Sqlite {
class SQLITE_EXPORT Exception : public std::exception class SQLITE_EXPORT Exception : public std::exception
{ {
public: public:
Exception(const char *whatErrorHasHappen, Utils::SmallString &&sqliteErrorMessage)
: m_whatErrorHasHappen(whatErrorHasHappen)
, m_sqliteErrorMessage(std::move(sqliteErrorMessage))
{}
Exception(const char *whatErrorHasHappen) Exception(const char *whatErrorHasHappen)
: m_whatErrorHasHappen(whatErrorHasHappen) : m_whatErrorHasHappen(whatErrorHasHappen)
{} {}
const char *what() const noexcept override { return m_whatErrorHasHappen; } const char *what() const noexcept override { return m_whatErrorHasHappen; }
private:
const char *m_whatErrorHasHappen;
};
class SQLITE_EXPORT ExceptionWithMessage : public Exception
{
public:
ExceptionWithMessage(const char *whatErrorHasHappen,
Utils::SmallString &&sqliteErrorMessage = Utils::SmallString{})
: Exception{whatErrorHasHappen}
, m_sqliteErrorMessage(std::move(sqliteErrorMessage))
{}
void printWarning() const; void printWarning() const;
private: private:
const char *m_whatErrorHasHappen;
Utils::SmallString m_sqliteErrorMessage; Utils::SmallString m_sqliteErrorMessage;
}; };
class StatementIsBusy : public Exception class StatementIsBusy : public ExceptionWithMessage
{ {
public: public:
StatementIsBusy(const char *whatErrorHasHappen, using ExceptionWithMessage::ExceptionWithMessage;
Utils::SmallString &&sqliteErrorMessage = Utils::SmallString())
: Exception(whatErrorHasHappen, std::move(sqliteErrorMessage))
{}
}; };
class DatabaseIsBusy : public Exception class DatabaseIsBusy : public Exception
{ {
public: public:
DatabaseIsBusy(const char *whatErrorHasHappen) using Exception::Exception;
: Exception(whatErrorHasHappen)
{}
}; };
class StatementHasError : public Exception class StatementHasError : public ExceptionWithMessage
{ {
public: public:
StatementHasError(const char *whatErrorHasHappen, using ExceptionWithMessage::ExceptionWithMessage;
Utils::SmallString &&sqliteErrorMessage = Utils::SmallString())
: Exception(whatErrorHasHappen, std::move(sqliteErrorMessage))
{}
}; };
class StatementIsMisused : public Exception class StatementIsMisused : public ExceptionWithMessage
{ {
public: public:
StatementIsMisused(const char *whatErrorHasHappen, using ExceptionWithMessage::ExceptionWithMessage;
Utils::SmallString &&sqliteErrorMessage = Utils::SmallString())
: Exception(whatErrorHasHappen, std::move(sqliteErrorMessage))
{}
}; };
class InputOutputError : public Exception class InputOutputError : public Exception
{ {
public: public:
InputOutputError(const char *whatErrorHasHappen) using Exception::Exception;
: Exception(whatErrorHasHappen)
{}
}; };
class ConstraintPreventsModification : public Exception class ConstraintPreventsModification : public ExceptionWithMessage
{ {
public: public:
ConstraintPreventsModification(const char *whatErrorHasHappen, using ExceptionWithMessage::ExceptionWithMessage;
Utils::SmallString &&sqliteErrorMessage = Utils::SmallString())
: Exception(whatErrorHasHappen, std::move(sqliteErrorMessage))
{}
}; };
class NoValuesToFetch : public Exception class NoValuesToFetch : public Exception
{ {
public: public:
NoValuesToFetch(const char *whatErrorHasHappen) using Exception::Exception;
: Exception(whatErrorHasHappen)
{}
}; };
class ColumnCountDoesNotMatch : public Exception class ColumnCountDoesNotMatch : public Exception
{ {
public: public:
ColumnCountDoesNotMatch(const char *whatErrorHasHappen) using Exception::Exception;
: Exception(whatErrorHasHappen)
{}
}; };
class BindingIndexIsOutOfRange : public Exception class BindingIndexIsOutOfRange : public ExceptionWithMessage
{ {
public: public:
BindingIndexIsOutOfRange(const char *whatErrorHasHappen, using ExceptionWithMessage::ExceptionWithMessage;
Utils::SmallString &&sqliteErrorMessage = Utils::SmallString())
: Exception(whatErrorHasHappen, std::move(sqliteErrorMessage))
{}
}; };
class WrongBindingName : public Exception class WrongBindingName : public Exception
{ {
public: public:
WrongBindingName(const char *whatErrorHasHappen) using Exception::Exception;
: Exception(whatErrorHasHappen)
{}
}; };
class DatabaseIsNotOpen : public Exception class DatabaseIsNotOpen : public Exception
{ {
public: public:
DatabaseIsNotOpen(const char *whatErrorHasHappen) using Exception::Exception;
: Exception(whatErrorHasHappen)
{}
}; };
class DatabaseCannotBeOpened : public Exception class DatabaseCannotBeOpened : public ExceptionWithMessage
{ {
public: public:
DatabaseCannotBeOpened(const char *whatErrorHasHappen, using ExceptionWithMessage::ExceptionWithMessage;
Utils::SmallString &&errorMessage = Utils::SmallString())
: Exception(whatErrorHasHappen, std::move(errorMessage))
{}
}; };
class DatabaseFilePathIsEmpty : public DatabaseCannotBeOpened class DatabaseFilePathIsEmpty : public DatabaseCannotBeOpened
{ {
public: public:
DatabaseFilePathIsEmpty(const char *whatErrorHasHappen) using DatabaseCannotBeOpened::DatabaseCannotBeOpened;
: DatabaseCannotBeOpened(whatErrorHasHappen)
{}
}; };
class DatabaseIsAlreadyOpen : public DatabaseCannotBeOpened class DatabaseIsAlreadyOpen : public DatabaseCannotBeOpened
{ {
public: public:
DatabaseIsAlreadyOpen(const char *whatErrorHasHappen) using DatabaseCannotBeOpened::DatabaseCannotBeOpened;
: DatabaseCannotBeOpened(whatErrorHasHappen)
{}
}; };
class DatabaseCannotBeClosed : public Exception class DatabaseCannotBeClosed : public Exception
{ {
public: public:
DatabaseCannotBeClosed(const char *whatErrorHasHappen) using Exception::Exception;
: Exception(whatErrorHasHappen)
{}
}; };
class DatabaseIsAlreadyClosed : public DatabaseCannotBeClosed class DatabaseIsAlreadyClosed : public DatabaseCannotBeClosed
{ {
public: public:
DatabaseIsAlreadyClosed(const char *whatErrorHasHappen) using DatabaseCannotBeClosed::DatabaseCannotBeClosed;
: DatabaseCannotBeClosed(whatErrorHasHappen)
{}
}; };
class WrongFilePath : public DatabaseCannotBeOpened class WrongFilePath : public ExceptionWithMessage
{ {
public: public:
WrongFilePath(const char *whatErrorHasHappen, using ExceptionWithMessage::ExceptionWithMessage;
Utils::SmallString &&errorMessage = Utils::SmallString())
: DatabaseCannotBeOpened(whatErrorHasHappen, std::move(errorMessage))
{}
}; };
class PragmaValueNotSet : public Exception class PragmaValueNotSet : public Exception
{ {
public: public:
PragmaValueNotSet(const char *whatErrorHasHappen) using Exception::Exception;
: Exception(whatErrorHasHappen)
{}
}; };
class NotReadOnlySqlStatement : public Exception class NotReadOnlySqlStatement : public Exception
{ {
public: public:
NotReadOnlySqlStatement(const char *whatErrorHasHappen) using Exception::Exception;
: Exception(whatErrorHasHappen)
{}
}; };
class NotWriteSqlStatement : public Exception class NotWriteSqlStatement : public Exception
{ {
public: public:
NotWriteSqlStatement(const char *whatErrorHasHappen) using Exception::Exception;
: Exception(whatErrorHasHappen)
{}
}; };
class DeadLock : public Exception class DeadLock : public Exception
{ {
public: public:
DeadLock(const char *whatErrorHasHappen) using Exception::Exception;
: Exception(whatErrorHasHappen)
{}
}; };
class UnknowError : public Exception class UnknowError : public ExceptionWithMessage
{ {
public: public:
UnknowError(const char *whatErrorHasHappen, using ExceptionWithMessage::ExceptionWithMessage;
Utils::SmallString &&errorMessage = Utils::SmallString())
: Exception(whatErrorHasHappen, std::move(errorMessage))
{}
}; };
class BindingTooBig : public Exception class BindingTooBig : public ExceptionWithMessage
{ {
public: public:
BindingTooBig(const char *whatErrorHasHappen, using ExceptionWithMessage::ExceptionWithMessage;
Utils::SmallString &&errorMessage = Utils::SmallString())
: Exception(whatErrorHasHappen, std::move(errorMessage))
{}
}; };
class TooBig : public Exception class TooBig : public ExceptionWithMessage
{ {
public: public:
TooBig(const char *whatErrorHasHappen, Utils::SmallString &&errorMessage = Utils::SmallString()) using ExceptionWithMessage::ExceptionWithMessage;
: Exception(whatErrorHasHappen, std::move(errorMessage))
{}
}; };
class CannotConvert : public Exception class CannotConvert : public Exception
{ {
public: public:
CannotConvert(const char *whatErrorHasHappen) using Exception::Exception;
: Exception(whatErrorHasHappen)
{}
}; };
class ForeignKeyColumnIsNotUnique : public Exception class ForeignKeyColumnIsNotUnique : public Exception
{ {
public: public:
ForeignKeyColumnIsNotUnique(const char *whatErrorHasHappen) using Exception::Exception;
: Exception(whatErrorHasHappen)
{}
}; };
class CannotApplyChangeSet : public Exception class CannotApplyChangeSet : public Exception
{ {
public: public:
CannotApplyChangeSet(const char *whatErrorHasHappen) using Exception::Exception;
: Exception(whatErrorHasHappen)
{}
}; };
class ChangeSetIsMisused : public Exception class ChangeSetIsMisused : public Exception
{ {
public: public:
ChangeSetIsMisused(const char *whatErrorHasHappen) using Exception::Exception;
: Exception(whatErrorHasHappen)
{}
}; };
class SchemeChangeError : public Exception class SchemeChangeError : public ExceptionWithMessage
{ {
public: public:
SchemeChangeError(const char *whatErrorHasHappen, using ExceptionWithMessage::ExceptionWithMessage;
Utils::SmallString &&errorMessage = Utils::SmallString())
: Exception(whatErrorHasHappen, std::move(errorMessage))
{}
}; };
class CannotWriteToReadOnlyConnection : public Exception class CannotWriteToReadOnlyConnection : public ExceptionWithMessage
{ {
public: public:
CannotWriteToReadOnlyConnection(const char *whatErrorHasHappen, using ExceptionWithMessage::ExceptionWithMessage;
Utils::SmallString &&errorMessage = Utils::SmallString())
: Exception(whatErrorHasHappen, std::move(errorMessage))
{}
}; };
class ProtocolError : public Exception class ProtocolError : public ExceptionWithMessage
{ {
public: public:
ProtocolError(const char *whatErrorHasHappen, using ExceptionWithMessage::ExceptionWithMessage;
Utils::SmallString &&errorMessage = Utils::SmallString())
: Exception(whatErrorHasHappen, std::move(errorMessage))
{}
}; };
class DatabaseExceedsMaximumFileSize : public Exception class DatabaseExceedsMaximumFileSize : public ExceptionWithMessage
{ {
public: public:
DatabaseExceedsMaximumFileSize(const char *whatErrorHasHappen, using ExceptionWithMessage::ExceptionWithMessage;
Utils::SmallString &&errorMessage = Utils::SmallString())
: Exception(whatErrorHasHappen, std::move(errorMessage))
{}
}; };
class DataTypeMismatch : public Exception class DataTypeMismatch : public ExceptionWithMessage
{ {
public: public:
DataTypeMismatch(const char *whatErrorHasHappen, using ExceptionWithMessage::ExceptionWithMessage;
Utils::SmallString &&errorMessage = Utils::SmallString())
: Exception(whatErrorHasHappen, std::move(errorMessage))
{}
}; };
class ConnectionIsLocked : public Exception class ConnectionIsLocked : public ExceptionWithMessage
{ {
public: public:
ConnectionIsLocked(const char *whatErrorHasHappen, using ExceptionWithMessage::ExceptionWithMessage;
Utils::SmallString &&errorMessage = Utils::SmallString())
: Exception(whatErrorHasHappen, std::move(errorMessage))
{}
}; };
class ExecutionInterrupted : public Exception class ExecutionInterrupted : public ExceptionWithMessage
{ {
public: public:
ExecutionInterrupted(const char *whatErrorHasHappen, using ExceptionWithMessage::ExceptionWithMessage;
Utils::SmallString &&errorMessage = Utils::SmallString())
: Exception(whatErrorHasHappen, std::move(errorMessage))
{}
}; };
class DatabaseIsCorrupt : public Exception class DatabaseIsCorrupt : public ExceptionWithMessage
{ {
public: public:
DatabaseIsCorrupt(const char *whatErrorHasHappen, using ExceptionWithMessage::ExceptionWithMessage;
Utils::SmallString &&errorMessage = Utils::SmallString())
: Exception(whatErrorHasHappen, std::move(errorMessage))
{}
}; };
class CannotOpen : public Exception class CannotOpen : public ExceptionWithMessage
{ {
public: public:
CannotOpen(const char *whatErrorHasHappen, using ExceptionWithMessage::ExceptionWithMessage;
Utils::SmallString &&errorMessage = Utils::SmallString())
: Exception(whatErrorHasHappen, std::move(errorMessage))
{}
}; };
class CannotCreateChangeSetIterator : public Exception class CannotCreateChangeSetIterator : public Exception
{ {
public: public:
CannotCreateChangeSetIterator(const char *whatErrorHasHappen) using Exception::Exception;
: Exception(whatErrorHasHappen)
{}
}; };
class CannotGetChangeSetOperation : public Exception class CannotGetChangeSetOperation : public Exception
{ {
public: public:
CannotGetChangeSetOperation(const char *whatErrorHasHappen) using Exception::Exception;
: Exception(whatErrorHasHappen)
{}
}; };
class ChangeSetTupleIsOutOfRange : public Exception class ChangeSetTupleIsOutOfRange : public Exception
{ {
public: public:
ChangeSetTupleIsOutOfRange(const char *whatErrorHasHappen) using Exception::Exception;
: Exception(whatErrorHasHappen)
{}
}; };
class ChangeSetTupleIsMisused : public Exception class ChangeSetTupleIsMisused : public Exception
{ {
public: public:
ChangeSetTupleIsMisused(const char *whatErrorHasHappen) using Exception::Exception;
: Exception(whatErrorHasHappen)
{}
}; };
class UnknownError : public Exception class UnknownError : public Exception
{ {
public: public:
UnknownError(const char *whatErrorHasHappen) using Exception::Exception;
: Exception(whatErrorHasHappen)
{}
}; };
class DatabaseIsNotLocked : public Exception class DatabaseIsNotLocked : public Exception
{ {
public: public:
DatabaseIsNotLocked(const char *whatErrorHasHappen) using Exception::Exception;
: Exception(whatErrorHasHappen) };
{}
class WrongBindingParameterCount : public Exception
{
public:
using Exception::Exception;
}; };
} // namespace Sqlite } // namespace Sqlite

View File

@@ -29,10 +29,10 @@
namespace Sqlite { namespace Sqlite {
class SQLITE_EXPORT SqlStatementBuilderException : public Exception class SQLITE_EXPORT SqlStatementBuilderException : public ExceptionWithMessage
{ {
public: public:
using Exception::Exception; using ExceptionWithMessage::ExceptionWithMessage;
}; };
} // namespace Sqlite } // namespace Sqlite