Commit 37ff00eb authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

sqlite: Rework exceptions class

So all sqlite error have a common base class, and the error code can
be queried
parent d030ea87
......@@ -24,6 +24,7 @@
#include <string>
#include <exception>
#include <stdexcept>
namespace medialibrary
{
......@@ -33,39 +34,62 @@ namespace sqlite
namespace errors
{
class ConstraintViolation : public std::exception
class Generic : public std::runtime_error
{
public:
ConstraintViolation( const std::string& req, const std::string& err )
Generic( const char* req, const char* msg )
: std::runtime_error( std::string( "Failed to compile/prepare request <" ) + req
+ ">: " + msg )
{
m_reason = std::string( "Request <" ) + req + "> aborted due to "
"constraint violation (" + err + ")";
}
virtual const char* what() const noexcept override
Generic( const std::string& msg )
: std::runtime_error( msg )
{
return m_reason.c_str();
}
private:
std::string m_reason;
};
class ColumnOutOfRange : public std::exception
class GenericExecution : public Generic
{
public:
ColumnOutOfRange( unsigned int idx, unsigned int nbColumns )
GenericExecution( const char* req, const char* errMsg, int errCode )
: Generic( std::string( "Failed to run request <" ) + req + ">: " + errMsg )
, m_errorCode( errCode )
{
m_reason = "Attempting to extract column at index " + std::to_string( idx ) +
" from a request with " + std::to_string( nbColumns ) + " columns";
}
virtual const char* what() const noexcept override
GenericExecution( const std::string& msg, int errCode )
: Generic( msg )
, m_errorCode( errCode )
{
return m_reason.c_str();
}
int code() const
{
return m_errorCode;
}
private:
std::string m_reason;
int m_errorCode;
};
class ConstraintViolation : public Generic
{
public:
ConstraintViolation( const std::string& req, const std::string& err )
: Generic( std::string( "Request <" ) + req + "> aborted due to "
"constraint violation (" + err + ")" )
{
}
};
class ColumnOutOfRange : public Generic
{
public:
ColumnOutOfRange( unsigned int idx, unsigned int nbColumns )
: Generic( "Attempting to extract column at index " + std::to_string( idx ) +
" from a request with " + std::to_string( nbColumns ) + " columns" )
{
}
};
} // namespace errors
......
......@@ -135,8 +135,7 @@ public:
int res = sqlite3_prepare_v2( dbConnection, req.c_str(), -1, &stmt, NULL );
if ( res != SQLITE_OK )
{
throw std::runtime_error( std::string( "Failed to compile request: " ) + req + " " +
sqlite3_errmsg( dbConnection ) );
throw errors::Generic( req.c_str(), sqlite3_errmsg( dbConnection ) );
}
m_stmt.reset( stmt );
connMap.emplace( req, CachedStmtPtr( stmt, &sqlite3_finalize ) );
......@@ -169,14 +168,13 @@ public:
else if ( res == SQLITE_BUSY && ( Transaction::transactionInProgress() == false ||
m_isCommit == true ) && maxRetries-- > 0 )
continue;
std::string errMsg = sqlite3_errmsg( m_dbConn );
auto errMsg = sqlite3_errmsg( m_dbConn );
switch ( res )
{
case SQLITE_CONSTRAINT:
throw errors::ConstraintViolation( sqlite3_sql( m_stmt.get() ), errMsg );
default:
throw std::runtime_error( std::string{ sqlite3_sql( m_stmt.get() ) }
+ ": " + errMsg );
throw errors::GenericExecution( sqlite3_sql( m_stmt.get() ), errMsg, res );
}
}
}
......@@ -193,7 +191,7 @@ private:
{
auto res = Traits<T>::Bind( m_stmt.get(), m_bindIdx, std::forward<T>( value ) );
if ( res != SQLITE_OK )
throw std::runtime_error( "Failed to bind parameter" );
throw errors::Generic( sqlite3_sql( m_stmt.get() ), "Failed to bind parameter" );
m_bindIdx++;
return true;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment