aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2019-05-24 03:45:29 -0400
committerJack Lloyd <[email protected]>2019-05-24 03:45:29 -0400
commitf5c0d868f685c5e67c164b03cb8129985fd56bee (patch)
treee156f40e3566493e9469cc56b41e794a66e8daca /src/lib
parentdaa0e6f5af59135f0049ebdd7b666b13bd6b3224 (diff)
parent9e5ddd2358d07859da54365bca04bd71a938e68e (diff)
Merge GH #1971 Add error_type for database error
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/ffi/ffi.cpp2
-rw-r--r--src/lib/utils/database.h16
-rw-r--r--src/lib/utils/exceptn.cpp110
-rw-r--r--src/lib/utils/exceptn.h2
-rw-r--r--src/lib/utils/sqlite3/sqlite3.cpp13
5 files changed, 81 insertions, 62 deletions
diff --git a/src/lib/ffi/ffi.cpp b/src/lib/ffi/ffi.cpp
index 4fe1d8c71..bd05943e9 100644
--- a/src/lib/ffi/ffi.cpp
+++ b/src/lib/ffi/ffi.cpp
@@ -33,6 +33,7 @@ int ffi_map_error_type(Botan::ErrorType err)
{
case Botan::ErrorType::Unknown:
return BOTAN_FFI_ERROR_UNKNOWN_ERROR;
+
case Botan::ErrorType::SystemError:
case Botan::ErrorType::IoError:
case Botan::ErrorType::OpenSSLError:
@@ -42,6 +43,7 @@ int ffi_map_error_type(Botan::ErrorType err)
case Botan::ErrorType::ZlibError:
case Botan::ErrorType::Bzip2Error:
case Botan::ErrorType::LzmaError:
+ case Botan::ErrorType::DatabaseError:
return BOTAN_FFI_ERROR_SYSTEM_ERROR;
case Botan::ErrorType::NotImplemented:
diff --git a/src/lib/utils/database.h b/src/lib/utils/database.h
index 21a207445..713d4fc59 100644
--- a/src/lib/utils/database.h
+++ b/src/lib/utils/database.h
@@ -23,7 +23,21 @@ class BOTAN_PUBLIC_API(2,0) SQL_Database
class BOTAN_PUBLIC_API(2,0) SQL_DB_Error final : public Exception
{
public:
- explicit SQL_DB_Error(const std::string& what) : Exception("SQL database", what) {}
+ explicit SQL_DB_Error(const std::string& what) :
+ Exception("SQL database", what),
+ m_rc(0)
+ {}
+
+ SQL_DB_Error(const std::string& what, int rc) :
+ Exception("SQL database", what),
+ m_rc(rc)
+ {}
+
+ ErrorType error_type() const noexcept override { return Botan::ErrorType::DatabaseError; }
+
+ int error_code() const noexcept override { return m_rc; }
+ private:
+ int m_rc;
};
class BOTAN_PUBLIC_API(2,0) Statement
diff --git a/src/lib/utils/exceptn.cpp b/src/lib/utils/exceptn.cpp
index 3876c3b26..d5845e09d 100644
--- a/src/lib/utils/exceptn.cpp
+++ b/src/lib/utils/exceptn.cpp
@@ -9,62 +9,64 @@
namespace Botan {
std::string to_string(ErrorType type)
-{
- switch(type)
{
- case ErrorType::Unknown:
- return "Unknown";
- case ErrorType::SystemError:
- return "SystemError";
- case ErrorType::NotImplemented:
- return "NotImplemented";
- case ErrorType::OutOfMemory:
- return "OutOfMemory";
- case ErrorType::InternalError:
- return "InternalError";
- case ErrorType::IoError:
- return "IoError";
- case ErrorType::InvalidObjectState :
- return "InvalidObjectState";
- case ErrorType::KeyNotSet:
- return "KeyNotSet";
- case ErrorType::InvalidArgument:
- return "InvalidArgument";
- case ErrorType::InvalidKeyLength:
- return "InvalidKeyLength";
- case ErrorType::InvalidNonceLength:
- return "InvalidNonceLength";
- case ErrorType::LookupError:
- return "LookupError";
- case ErrorType::EncodingFailure:
- return "EncodingFailure";
- case ErrorType::DecodingFailure:
- return "DecodingFailure";
- case ErrorType::TLSError:
- return "TLSError";
- case ErrorType::HttpError:
- return "HttpError";
- case ErrorType::InvalidTag:
- return "InvalidTag";
- case ErrorType::OpenSSLError :
- return "OpenSSLError";
- case ErrorType::CommonCryptoError:
- return "CommonCryptoError";
- case ErrorType::Pkcs11Error:
- return "Pkcs11Error";
- case ErrorType::TPMError:
- return "TPMError";
- case ErrorType::ZlibError :
- return "ZlibError";
- case ErrorType::Bzip2Error:
- return "Bzip2Error" ;
- case ErrorType::LzmaError:
- return "LzmaError";
-
- default:
- return "unrecognized botan error";
+ switch(type)
+ {
+ case ErrorType::Unknown:
+ return "Unknown";
+ case ErrorType::SystemError:
+ return "SystemError";
+ case ErrorType::NotImplemented:
+ return "NotImplemented";
+ case ErrorType::OutOfMemory:
+ return "OutOfMemory";
+ case ErrorType::InternalError:
+ return "InternalError";
+ case ErrorType::IoError:
+ return "IoError";
+ case ErrorType::InvalidObjectState :
+ return "InvalidObjectState";
+ case ErrorType::KeyNotSet:
+ return "KeyNotSet";
+ case ErrorType::InvalidArgument:
+ return "InvalidArgument";
+ case ErrorType::InvalidKeyLength:
+ return "InvalidKeyLength";
+ case ErrorType::InvalidNonceLength:
+ return "InvalidNonceLength";
+ case ErrorType::LookupError:
+ return "LookupError";
+ case ErrorType::EncodingFailure:
+ return "EncodingFailure";
+ case ErrorType::DecodingFailure:
+ return "DecodingFailure";
+ case ErrorType::TLSError:
+ return "TLSError";
+ case ErrorType::HttpError:
+ return "HttpError";
+ case ErrorType::InvalidTag:
+ return "InvalidTag";
+ case ErrorType::OpenSSLError :
+ return "OpenSSLError";
+ case ErrorType::CommonCryptoError:
+ return "CommonCryptoError";
+ case ErrorType::Pkcs11Error:
+ return "Pkcs11Error";
+ case ErrorType::TPMError:
+ return "TPMError";
+ case ErrorType::DatabaseError:
+ return "DatabaseError";
+ case ErrorType::ZlibError :
+ return "ZlibError";
+ case ErrorType::Bzip2Error:
+ return "Bzip2Error" ;
+ case ErrorType::LzmaError:
+ return "LzmaError";
+ }
+
+ // No default case in above switch so compiler warns
+ return "Unrecognized Botan error";
}
-}
Exception::Exception(const std::string& msg) : m_msg(msg)
{}
diff --git a/src/lib/utils/exceptn.h b/src/lib/utils/exceptn.h
index 0ca888316..0de17f6f3 100644
--- a/src/lib/utils/exceptn.h
+++ b/src/lib/utils/exceptn.h
@@ -62,6 +62,8 @@ enum class ErrorType {
Pkcs11Error,
/** An error when interacting with a TPM device */
TPMError,
+ /** An error when interacting with a database */
+ DatabaseError,
/** An error when interacting with zlib */
ZlibError = 300,
diff --git a/src/lib/utils/sqlite3/sqlite3.cpp b/src/lib/utils/sqlite3/sqlite3.cpp
index a1fdfaa21..8c71ecd96 100644
--- a/src/lib/utils/sqlite3/sqlite3.cpp
+++ b/src/lib/utils/sqlite3/sqlite3.cpp
@@ -67,24 +67,23 @@ Sqlite3_Database::Sqlite3_Statement::Sqlite3_Statement(sqlite3* db, const std::s
int rc = ::sqlite3_prepare_v2(db, base_sql.c_str(), -1, &m_stmt, nullptr);
if(rc != SQLITE_OK)
- throw SQL_DB_Error("sqlite3_prepare failed " + base_sql +
- ", code " + std::to_string(rc));
+ throw SQL_DB_Error("sqlite3_prepare failed on " + base_sql, rc);
}
void Sqlite3_Database::Sqlite3_Statement::bind(int column, const std::string& val)
{
int rc = ::sqlite3_bind_text(m_stmt, column, val.c_str(), -1, SQLITE_TRANSIENT);
if(rc != SQLITE_OK)
- throw SQL_DB_Error("sqlite3_bind_text failed, code " + std::to_string(rc));
+ throw SQL_DB_Error("sqlite3_bind_text failed", rc);
}
void Sqlite3_Database::Sqlite3_Statement::bind(int column, size_t val)
{
- if(val != static_cast<size_t>(static_cast<int>(val))) // is this legit?
+ if(val != static_cast<size_t>(static_cast<int>(val))) // is this cast legit?
throw SQL_DB_Error("sqlite3 cannot store " + std::to_string(val) + " without truncation");
int rc = ::sqlite3_bind_int(m_stmt, column, val);
if(rc != SQLITE_OK)
- throw SQL_DB_Error("sqlite3_bind_int failed, code " + std::to_string(rc));
+ throw SQL_DB_Error("sqlite3_bind_int failed", rc);
}
void Sqlite3_Database::Sqlite3_Statement::bind(int column, std::chrono::system_clock::time_point time)
@@ -97,14 +96,14 @@ void Sqlite3_Database::Sqlite3_Statement::bind(int column, const std::vector<uin
{
int rc = ::sqlite3_bind_blob(m_stmt, column, val.data(), val.size(), SQLITE_TRANSIENT);
if(rc != SQLITE_OK)
- throw SQL_DB_Error("sqlite3_bind_text failed, code " + std::to_string(rc));
+ throw SQL_DB_Error("sqlite3_bind_text failed", rc);
}
void Sqlite3_Database::Sqlite3_Statement::bind(int column, const uint8_t* p, size_t len)
{
int rc = ::sqlite3_bind_blob(m_stmt, column, p, len, SQLITE_TRANSIENT);
if(rc != SQLITE_OK)
- throw SQL_DB_Error("sqlite3_bind_text failed, code " + std::to_string(rc));
+ throw SQL_DB_Error("sqlite3_bind_text failed", rc);
}
std::pair<const uint8_t*, size_t> Sqlite3_Database::Sqlite3_Statement::get_blob(int column)