refactor database exceptions

This commit is contained in:
Chris Cromer 2022-12-10 21:11:39 -03:00
parent 541ae2db28
commit 74de3c67c0
Signed by: cromer
GPG Key ID: FA91071797BEEEC2
5 changed files with 102 additions and 144 deletions

View File

@ -93,7 +93,7 @@ void obelisk::KnowledgeBase::addEntities(std::vector<obelisk::Entity>& entities)
{ {
entity.insertEntity(dbConnection_); entity.insertEntity(dbConnection_);
} }
catch (obelisk::DatabaseException::ConstraintException& exception) catch (obelisk::DatabaseConstraintException& exception)
{ {
// ignore unique constraint error // ignore unique constraint error
if (std::strcmp(exception.what(), if (std::strcmp(exception.what(),
@ -114,7 +114,7 @@ void obelisk::KnowledgeBase::addVerbs(std::vector<obelisk::Verb>& verbs)
{ {
verb.insertVerb(dbConnection_); verb.insertVerb(dbConnection_);
} }
catch (obelisk::DatabaseException::ConstraintException& exception) catch (obelisk::DatabaseConstraintException& exception)
{ {
// ignore unique constraint error // ignore unique constraint error
if (std::strcmp(exception.what(), if (std::strcmp(exception.what(),
@ -135,7 +135,7 @@ void obelisk::KnowledgeBase::addFacts(std::vector<obelisk::Fact>& facts)
{ {
fact.insertFact(dbConnection_); fact.insertFact(dbConnection_);
} }
catch (obelisk::DatabaseException::ConstraintException& exception) catch (obelisk::DatabaseConstraintException& exception)
{ {
// ignore unique constraint error // ignore unique constraint error
if (std::strcmp(exception.what(), if (std::strcmp(exception.what(),

View File

@ -38,13 +38,13 @@ void obelisk::Entity::selectEntity(sqlite3* dbConnection)
case SQLITE_OK : case SQLITE_OK :
break; break;
case SQLITE_TOOBIG : case SQLITE_TOOBIG :
throw obelisk::DatabaseException::SizeException(); throw obelisk::DatabaseSizeException();
break; break;
case SQLITE_RANGE : case SQLITE_RANGE :
throw obelisk::DatabaseException::RangeException(); throw obelisk::DatabaseRangeException();
break; break;
case SQLITE_NOMEM : case SQLITE_NOMEM :
throw obelisk::DatabaseException::MemoryException(); throw obelisk::DatabaseMemoryException();
break; break;
default : default :
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
@ -62,10 +62,10 @@ void obelisk::Entity::selectEntity(sqlite3* dbConnection)
setName((char*) sqlite3_column_text(ppStmt, 1)); setName((char*) sqlite3_column_text(ppStmt, 1));
break; break;
case SQLITE_BUSY : case SQLITE_BUSY :
throw obelisk::DatabaseException::BusyException(); throw obelisk::DatabaseBusyException();
break; break;
case SQLITE_MISUSE : case SQLITE_MISUSE :
throw obelisk::DatabaseException::MisuseException(); throw obelisk::DatabaseMisuseException();
break; break;
default : default :
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
@ -106,13 +106,13 @@ void obelisk::Entity::insertEntity(sqlite3* dbConnection)
case SQLITE_OK : case SQLITE_OK :
break; break;
case SQLITE_TOOBIG : case SQLITE_TOOBIG :
throw obelisk::DatabaseException::SizeException(); throw obelisk::DatabaseSizeException();
break; break;
case SQLITE_RANGE : case SQLITE_RANGE :
throw obelisk::DatabaseException::RangeException(); throw obelisk::DatabaseRangeException();
break; break;
case SQLITE_NOMEM : case SQLITE_NOMEM :
throw obelisk::DatabaseException::MemoryException(); throw obelisk::DatabaseMemoryException();
break; break;
default : default :
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
@ -127,13 +127,13 @@ void obelisk::Entity::insertEntity(sqlite3* dbConnection)
sqlite3_set_last_insert_rowid(dbConnection, 0); sqlite3_set_last_insert_rowid(dbConnection, 0);
break; break;
case SQLITE_CONSTRAINT : case SQLITE_CONSTRAINT :
throw obelisk::DatabaseException::ConstraintException( throw obelisk::DatabaseConstraintException(
sqlite3_errmsg(dbConnection)); sqlite3_errmsg(dbConnection));
case SQLITE_BUSY : case SQLITE_BUSY :
throw obelisk::DatabaseException::BusyException(); throw obelisk::DatabaseBusyException();
break; break;
case SQLITE_MISUSE : case SQLITE_MISUSE :
throw obelisk::DatabaseException::MisuseException(); throw obelisk::DatabaseMisuseException();
break; break;
default : default :
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));

View File

@ -8,8 +8,8 @@ namespace obelisk
{ {
class DatabaseException : public std::exception class DatabaseException : public std::exception
{ {
private: protected:
const std::string errorMessage_; std::string errorMessage_;
public: public:
DatabaseException() : DatabaseException() :
@ -28,118 +28,76 @@ namespace obelisk
{ {
} }
const char* what() const noexcept virtual const char* what() const noexcept
{ {
return errorMessage_.c_str(); return errorMessage_.c_str();
} }
class SizeException : public std::exception virtual void setErrorMessage(const std::string errorMessage)
{ {
private: errorMessage_ = errorMessage;
const std::string errorMessage_; }
};
public: class DatabaseSizeException : public obelisk::DatabaseException
SizeException() : {
errorMessage_("size of string or blob exceeds limits") public:
{ DatabaseSizeException()
}
const char* what() const noexcept
{
return errorMessage_.c_str();
}
};
class RangeException : public std::exception
{ {
private: setErrorMessage("size of string or blob exceeds limits");
const std::string errorMessage_; }
};
public: class DatabaseRangeException : public obelisk::DatabaseException
RangeException() : {
errorMessage_("parameter index is out of range") public:
{ DatabaseRangeException()
}
const char* what() const noexcept
{
return errorMessage_.c_str();
}
};
class MemoryException : public std::exception
{ {
private: setErrorMessage("parameter index is out of range");
const std::string errorMessage_; }
};
public: class DatabaseMemoryException : public obelisk::DatabaseException
MemoryException() : {
errorMessage_("not enough memory for operation") public:
{ DatabaseMemoryException()
}
const char* what() const noexcept
{
return errorMessage_.c_str();
}
};
class BusyException : public std::exception
{ {
private: setErrorMessage("not enough memory for operation");
const std::string errorMessage_; }
};
public: class DatabaseBusyException : public obelisk::DatabaseException
BusyException() : {
errorMessage_( public:
"database was busy and operation not performed") DatabaseBusyException()
{
}
const char* what() const noexcept
{
return errorMessage_.c_str();
}
};
class MisuseException : public std::exception
{ {
private: setErrorMessage(
const std::string errorMessage_; "database was busy and operation was not performed");
}
};
public: class DatabaseMisuseException : public obelisk::DatabaseException
MisuseException() : {
errorMessage_("misuse of the database routine") public:
{ DatabaseMisuseException()
}
const char* what() const noexcept
{
return errorMessage_.c_str();
}
};
class ConstraintException : public std::exception
{ {
private: setErrorMessage("misuse of the database routine");
const std::string errorMessage_; }
};
public: class DatabaseConstraintException : public obelisk::DatabaseException
ConstraintException() : {
errorMessage_("a constraint exception occurred") public:
{ DatabaseConstraintException()
} {
setErrorMessage("a constraint exception occurred");
}
ConstraintException(const std::string& errorMessage) : DatabaseConstraintException(const std::string& errorMessage)
errorMessage_(errorMessage) {
{ setErrorMessage(errorMessage);
} }
const char* what() const noexcept
{
return errorMessage_.c_str();
}
};
}; };
} // namespace obelisk } // namespace obelisk

View File

@ -43,13 +43,13 @@ void obelisk::Fact::selectFact(sqlite3* dbConnection)
case SQLITE_OK : case SQLITE_OK :
break; break;
case SQLITE_TOOBIG : case SQLITE_TOOBIG :
throw obelisk::DatabaseException::SizeException(); throw obelisk::DatabaseSizeException();
break; break;
case SQLITE_RANGE : case SQLITE_RANGE :
throw obelisk::DatabaseException::RangeException(); throw obelisk::DatabaseRangeException();
break; break;
case SQLITE_NOMEM : case SQLITE_NOMEM :
throw obelisk::DatabaseException::MemoryException(); throw obelisk::DatabaseMemoryException();
break; break;
default : default :
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
@ -62,13 +62,13 @@ void obelisk::Fact::selectFact(sqlite3* dbConnection)
case SQLITE_OK : case SQLITE_OK :
break; break;
case SQLITE_TOOBIG : case SQLITE_TOOBIG :
throw obelisk::DatabaseException::SizeException(); throw obelisk::DatabaseSizeException();
break; break;
case SQLITE_RANGE : case SQLITE_RANGE :
throw obelisk::DatabaseException::RangeException(); throw obelisk::DatabaseRangeException();
break; break;
case SQLITE_NOMEM : case SQLITE_NOMEM :
throw obelisk::DatabaseException::MemoryException(); throw obelisk::DatabaseMemoryException();
break; break;
default : default :
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
@ -81,13 +81,13 @@ void obelisk::Fact::selectFact(sqlite3* dbConnection)
case SQLITE_OK : case SQLITE_OK :
break; break;
case SQLITE_TOOBIG : case SQLITE_TOOBIG :
throw obelisk::DatabaseException::SizeException(); throw obelisk::DatabaseSizeException();
break; break;
case SQLITE_RANGE : case SQLITE_RANGE :
throw obelisk::DatabaseException::RangeException(); throw obelisk::DatabaseRangeException();
break; break;
case SQLITE_NOMEM : case SQLITE_NOMEM :
throw obelisk::DatabaseException::MemoryException(); throw obelisk::DatabaseMemoryException();
break; break;
default : default :
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
@ -107,10 +107,10 @@ void obelisk::Fact::selectFact(sqlite3* dbConnection)
getVerb().setId(sqlite3_column_int(ppStmt, 3)); getVerb().setId(sqlite3_column_int(ppStmt, 3));
break; break;
case SQLITE_BUSY : case SQLITE_BUSY :
throw obelisk::DatabaseException::BusyException(); throw obelisk::DatabaseBusyException();
break; break;
case SQLITE_MISUSE : case SQLITE_MISUSE :
throw obelisk::DatabaseException::MisuseException(); throw obelisk::DatabaseMisuseException();
break; break;
default : default :
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
@ -149,13 +149,13 @@ void obelisk::Fact::insertFact(sqlite3* dbConnection)
case SQLITE_OK : case SQLITE_OK :
break; break;
case SQLITE_TOOBIG : case SQLITE_TOOBIG :
throw obelisk::DatabaseException::SizeException(); throw obelisk::DatabaseSizeException();
break; break;
case SQLITE_RANGE : case SQLITE_RANGE :
throw obelisk::DatabaseException::RangeException(); throw obelisk::DatabaseRangeException();
break; break;
case SQLITE_NOMEM : case SQLITE_NOMEM :
throw obelisk::DatabaseException::MemoryException(); throw obelisk::DatabaseMemoryException();
break; break;
default : default :
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
@ -168,13 +168,13 @@ void obelisk::Fact::insertFact(sqlite3* dbConnection)
case SQLITE_OK : case SQLITE_OK :
break; break;
case SQLITE_TOOBIG : case SQLITE_TOOBIG :
throw obelisk::DatabaseException::SizeException(); throw obelisk::DatabaseSizeException();
break; break;
case SQLITE_RANGE : case SQLITE_RANGE :
throw obelisk::DatabaseException::RangeException(); throw obelisk::DatabaseRangeException();
break; break;
case SQLITE_NOMEM : case SQLITE_NOMEM :
throw obelisk::DatabaseException::MemoryException(); throw obelisk::DatabaseMemoryException();
break; break;
default : default :
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
@ -187,13 +187,13 @@ void obelisk::Fact::insertFact(sqlite3* dbConnection)
case SQLITE_OK : case SQLITE_OK :
break; break;
case SQLITE_TOOBIG : case SQLITE_TOOBIG :
throw obelisk::DatabaseException::SizeException(); throw obelisk::DatabaseSizeException();
break; break;
case SQLITE_RANGE : case SQLITE_RANGE :
throw obelisk::DatabaseException::RangeException(); throw obelisk::DatabaseRangeException();
break; break;
case SQLITE_NOMEM : case SQLITE_NOMEM :
throw obelisk::DatabaseException::MemoryException(); throw obelisk::DatabaseMemoryException();
break; break;
default : default :
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
@ -208,13 +208,13 @@ void obelisk::Fact::insertFact(sqlite3* dbConnection)
sqlite3_set_last_insert_rowid(dbConnection, 0); sqlite3_set_last_insert_rowid(dbConnection, 0);
break; break;
case SQLITE_CONSTRAINT : case SQLITE_CONSTRAINT :
throw obelisk::DatabaseException::ConstraintException( throw obelisk::DatabaseConstraintException(
sqlite3_errmsg(dbConnection)); sqlite3_errmsg(dbConnection));
case SQLITE_BUSY : case SQLITE_BUSY :
throw obelisk::DatabaseException::BusyException(); throw obelisk::DatabaseBusyException();
break; break;
case SQLITE_MISUSE : case SQLITE_MISUSE :
throw obelisk::DatabaseException::MisuseException(); throw obelisk::DatabaseMisuseException();
break; break;
default : default :
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));

View File

@ -39,13 +39,13 @@ void obelisk::Verb::selectVerb(sqlite3* dbConnection)
case SQLITE_OK : case SQLITE_OK :
break; break;
case SQLITE_TOOBIG : case SQLITE_TOOBIG :
throw obelisk::DatabaseException::SizeException(); throw obelisk::DatabaseSizeException();
break; break;
case SQLITE_RANGE : case SQLITE_RANGE :
throw obelisk::DatabaseException::RangeException(); throw obelisk::DatabaseRangeException();
break; break;
case SQLITE_NOMEM : case SQLITE_NOMEM :
throw obelisk::DatabaseException::MemoryException(); throw obelisk::DatabaseMemoryException();
break; break;
default : default :
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
@ -63,10 +63,10 @@ void obelisk::Verb::selectVerb(sqlite3* dbConnection)
setName((char*) sqlite3_column_text(ppStmt, 1)); setName((char*) sqlite3_column_text(ppStmt, 1));
break; break;
case SQLITE_BUSY : case SQLITE_BUSY :
throw obelisk::DatabaseException::BusyException(); throw obelisk::DatabaseBusyException();
break; break;
case SQLITE_MISUSE : case SQLITE_MISUSE :
throw obelisk::DatabaseException::MisuseException(); throw obelisk::DatabaseMisuseException();
break; break;
default : default :
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
@ -106,13 +106,13 @@ void obelisk::Verb::insertVerb(sqlite3* dbConnection)
case SQLITE_OK : case SQLITE_OK :
break; break;
case SQLITE_TOOBIG : case SQLITE_TOOBIG :
throw obelisk::DatabaseException::SizeException(); throw obelisk::DatabaseSizeException();
break; break;
case SQLITE_RANGE : case SQLITE_RANGE :
throw obelisk::DatabaseException::RangeException(); throw obelisk::DatabaseRangeException();
break; break;
case SQLITE_NOMEM : case SQLITE_NOMEM :
throw obelisk::DatabaseException::MemoryException(); throw obelisk::DatabaseMemoryException();
break; break;
default : default :
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));
@ -127,13 +127,13 @@ void obelisk::Verb::insertVerb(sqlite3* dbConnection)
sqlite3_set_last_insert_rowid(dbConnection, 0); sqlite3_set_last_insert_rowid(dbConnection, 0);
break; break;
case SQLITE_CONSTRAINT : case SQLITE_CONSTRAINT :
throw obelisk::DatabaseException::ConstraintException( throw obelisk::DatabaseConstraintException(
sqlite3_errmsg(dbConnection)); sqlite3_errmsg(dbConnection));
case SQLITE_BUSY : case SQLITE_BUSY :
throw obelisk::DatabaseException::BusyException(); throw obelisk::DatabaseBusyException();
break; break;
case SQLITE_MISUSE : case SQLITE_MISUSE :
throw obelisk::DatabaseException::MisuseException(); throw obelisk::DatabaseMisuseException();
break; break;
default : default :
throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection)); throw obelisk::DatabaseException(sqlite3_errmsg(dbConnection));