//  // //  #### # # # -= SQLiteX =-  // // # # ## ## Errors.h  // //  ## # ###  // //  ## # ### Error classification and checking utilities  // //  # # ## ##  // // #### ##### # # R1 (C)2005 Markus Ewald -> License.txt  // //  // #ifndef SQLITEX_ERRORS_H #define SQLITEX_ERRORS_H #include "SQLiteX/SQLiteX.h" #include #include /// Declares a new exception class #define SQLITEX_DECLAREEXCEPTION(BASE, EXCEPTION) \ class EXCEPTION : public BASE { \ public: \ /** @brief Constructor */ \ SQLITEX_API EXCEPTION(int ErrorCode, const std::string &sWhat) : \ BASE(ErrorCode, sWhat) {} \ \ /** @brief Destructor */ \ SQLITEX_API virtual ~EXCEPTION() {} \ } namespace SQLiteX { //  // //  SQLiteX::Error  // //  // /// Root SQLiteX error /** Serves as the root class for all exceptions originating from SQLiteX */ class Error : public std::exception { public: /// Constructor SQLITEX_API Error(int ErrorCode, const std::string &sWhat) : std::exception(sWhat.c_str()), m_ErrorCode(ErrorCode) {} /// Destructor SQLITEX_API virtual ~Error() {} // // Error implementation // public: /// Get sqlite error code SQLITEX_API int getErrorCode() const { return m_ErrorCode; } private: int m_ErrorCode; }; //  // //  SQLiteX::  // //  // /// General error accessing the database file /** Thrown when an error occured while trying to open or access the database file */ SQLITEX_DECLAREEXCEPTION(Error, AccessError); // SQLITE_CANTOPEN /// Access permission denied /** Thrown when the permission to execute a specified operation on the database was denied by sqlite */ SQLITEX_DECLAREEXCEPTION(AccessError, PermissionDeniedError); // SQLITE_PERM /// Database has been corrupted /** Thrown when a database file that was attempted to be opened is corrupted */ SQLITEX_DECLAREEXCEPTION(AccessError, DatabaseCorruptedError); // SQLITE_CORRUPT /// File is not a database /** Thrown when the file that was attempted to be opened is not a sqlite database */ SQLITEX_DECLAREEXCEPTION(AccessError, NoDatabaseError); // SQLITE_NOTADB /// Disk access error /** Thrown when an io error occurs accessing the database file through the operating systems file system support routines */ SQLITEX_DECLAREEXCEPTION(AccessError, DiskIOError); // SQLITE_IOERR /// SQLite library not used correctly /** Special exception which denotes that the actions trying to perform are not allowed in the current context or that an SQL statement contains a plain syntax error */ SQLITEX_DECLAREEXCEPTION(Error, MisuseError); // SQLITE_MISUSE /// Data type does not match /** Thrown when one or more of the data types provided in a insertion or query do not match the excepted data type */ SQLITEX_DECLAREEXCEPTION(MisuseError, TypeMismatchError); // SQLITE_MISMATCH /// The database is currently busy /** Thrown when the operation could not be executed due to the database being busy doing other work */ SQLITEX_DECLAREEXCEPTION(Error, BusyError); // SQLITE_BUSY // ############################################################################################# // // # SQLiteX::checkError() # // // ############################################################################################# // /// Check return value of an sqlite function /** Helper function which translates SQLite error code to exceptions @param pSQLiteDB Database on which the checked function was called @param ErrorCode Error code to be checked @return The error code as it was given to the function */ inline int checkError(sqlite3 *pSQLiteDB, int ErrorCode) { switch(ErrorCode) { case 0: case SQLITE_ROW: case SQLITE_DONE: { return ErrorCode; } default: { std::string sErrorMessage = ::sqlite3_errmsg(pSQLiteDB); switch(ErrorCode) { case SQLITE_CANTOPEN: throw AccessError(ErrorCode, sErrorMessage); case SQLITE_PERM: throw PermissionDeniedError(ErrorCode, sErrorMessage); case SQLITE_CORRUPT: throw DatabaseCorruptedError(ErrorCode, sErrorMessage); case SQLITE_NOTADB: throw NoDatabaseError(ErrorCode, sErrorMessage); case SQLITE_IOERR: throw DiskIOError(ErrorCode, sErrorMessage); case SQLITE_MISUSE: throw MisuseError(ErrorCode, sErrorMessage); case SQLITE_MISMATCH: throw TypeMismatchError(ErrorCode, sErrorMessage); case SQLITE_BUSY: throw BusyError(ErrorCode, sErrorMessage); default: throw Error(ErrorCode, sErrorMessage); } // switch ErrorCode } } // switch ErrorCode } } // namespace SQLiteX #endif // SQLITEX_ERRORS_H