obelisk/src/lib/models/error.h

194 lines
5.0 KiB
C
Raw Normal View History

#ifndef OBELISK_MODELS_ERROR_H
#define OBELISK_MODELS_ERROR_H
#include <exception>
#include <string>
namespace obelisk
{
/**
* @brief Exception thrown by database models.
*
*/
class DatabaseException : public std::exception
{
2022-12-10 21:11:39 -03:00
protected:
/**
* @brief The error message describing the exception.
*
*/
2022-12-10 21:11:39 -03:00
std::string errorMessage_;
public:
/**
* @brief Construct a new DatabaseException object.
*
*/
DatabaseException() :
errorMessage_("an unknown error ocurred")
{
}
/**
* @brief Construct a new DatabaseException object.
*
* @param[in] errorCode The error code that came from sqlite.
*/
DatabaseException(const int errorCode) :
errorMessage_(
"database error " + std::to_string(errorCode) + " ocurred")
{
}
/**
* @brief Construct a new DatabaseException object.
*
* @param[in] errorMessage The error message to describe the
* exception.
*/
DatabaseException(const std::string& errorMessage) :
errorMessage_(errorMessage)
{
}
/**
* @brief Retreive the exception message as a C type string.
*
* @return const char* The error message.
*/
2022-12-10 21:11:39 -03:00
virtual const char* what() const noexcept
{
return errorMessage_.c_str();
}
/**
* @brief Set the error message.
*
* @param[in] errorMessage The error message.
*/
2022-12-10 21:11:39 -03:00
virtual void setErrorMessage(const std::string errorMessage)
{
2022-12-10 21:11:39 -03:00
errorMessage_ = errorMessage;
}
};
/**
* @brief Exception thrown if the string or blob size exceeds sqlite's
* limits.
*
*/
2022-12-10 21:11:39 -03:00
class DatabaseSizeException : public obelisk::DatabaseException
{
public:
/**
* @brief Construct a new DatabaseSizeException object.
*
*/
2022-12-10 21:11:39 -03:00
DatabaseSizeException()
{
setErrorMessage("size of string or blob exceeds limits");
}
};
/**
* @brief Exception thrown if the index used it out of range.
*
*/
2022-12-10 21:11:39 -03:00
class DatabaseRangeException : public obelisk::DatabaseException
{
public:
/**
* @brief Construct a new DatabaseRangeException object.
*
*/
2022-12-10 21:11:39 -03:00
DatabaseRangeException()
{
2022-12-10 21:11:39 -03:00
setErrorMessage("parameter index is out of range");
}
};
/**
* @brief Exception thrown if there is not enough memory to perform the
* operation.
*
*/
2022-12-10 21:11:39 -03:00
class DatabaseMemoryException : public obelisk::DatabaseException
{
public:
/**
* @brief Construct a new DatabaseMemoryException object.
*
*/
2022-12-10 21:11:39 -03:00
DatabaseMemoryException()
{
setErrorMessage("not enough memory for operation");
}
};
/**
* @brief Exception thrown if the database was busy.
*
*/
2022-12-10 21:11:39 -03:00
class DatabaseBusyException : public obelisk::DatabaseException
{
public:
/**
* @brief Construct a new DatabaseBusyException object.
*
*/
2022-12-10 21:11:39 -03:00
DatabaseBusyException()
{
setErrorMessage(
"database was busy and operation was not performed");
2022-12-10 21:11:39 -03:00
}
};
/**
* @brief Exception thrown if there is a misuse of the databse.
*
*/
2022-12-10 21:11:39 -03:00
class DatabaseMisuseException : public obelisk::DatabaseException
{
public:
/**
* @brief Construct a new DatabaseMisuseException object.
*
*/
2022-12-10 21:11:39 -03:00
DatabaseMisuseException()
{
2022-12-10 21:11:39 -03:00
setErrorMessage("misuse of the database routine");
}
};
/**
* @brief Exception thrown if a constraint was violated.
*
*/
2022-12-10 21:11:39 -03:00
class DatabaseConstraintException : public obelisk::DatabaseException
{
public:
/**
* @brief Construct a new DatabaseConstraintException object.
*
*/
2022-12-10 21:11:39 -03:00
DatabaseConstraintException()
{
2022-12-10 21:11:39 -03:00
setErrorMessage("a constraint exception occurred");
}
/**
* @brief Construct a new DatabaseConstraintException object.
*
* @param[in] errorMessage The error message to send when the
* constraint is violated.
*/
2022-12-10 21:11:39 -03:00
DatabaseConstraintException(const std::string& errorMessage)
{
2022-12-10 21:11:39 -03:00
setErrorMessage(errorMessage);
}
};
} // namespace obelisk
#endif