From c34910030cdcd6f1c1ff59af69145e63df8669fa Mon Sep 17 00:00:00 2001 From: Zachary Michaels Date: Fri, 31 Jan 2020 11:07:09 -0800 Subject: [PATCH] Improve SQLite error messages (#269) Signed-off-by: Zachary Michaels --- .../sqlite/sqlite_statement_wrapper.cpp | 30 ++++++++++++++----- .../sqlite/sqlite_wrapper.cpp | 14 +++++++-- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/rosbag2_storage_default_plugins/src/rosbag2_storage_default_plugins/sqlite/sqlite_statement_wrapper.cpp b/rosbag2_storage_default_plugins/src/rosbag2_storage_default_plugins/sqlite/sqlite_statement_wrapper.cpp index 5a8f4ce394..9604b17aaa 100644 --- a/rosbag2_storage_default_plugins/src/rosbag2_storage_default_plugins/sqlite/sqlite_statement_wrapper.cpp +++ b/rosbag2_storage_default_plugins/src/rosbag2_storage_default_plugins/sqlite/sqlite_statement_wrapper.cpp @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -33,8 +34,11 @@ SqliteStatementWrapper::SqliteStatementWrapper(sqlite3 * database, const std::st sqlite3_stmt * statement; int return_code = sqlite3_prepare_v2(database, query.c_str(), -1, &statement, nullptr); if (return_code != SQLITE_OK) { - throw SqliteException("SQL error when preparing statement '" + query + "' with return code: " + - std::to_string(return_code)); + std::stringstream errmsg; + errmsg << "Error when preparing SQL statement '" << query << "'. SQLite error: (" << + return_code << "): " << sqlite3_errstr(return_code); + + throw SqliteException{errmsg.str()}; } statement_ = statement; @@ -52,8 +56,11 @@ std::shared_ptr SqliteStatementWrapper::execute_and_rese { int return_code = sqlite3_step(statement_); if (!is_query_ok(return_code)) { - throw SqliteException("Error processing SQLite statement. Return code: " + - std::to_string(return_code)); + std::stringstream errmsg; + errmsg << "Error when processing SQL statement. SQLite error (" << + return_code << "): " << sqlite3_errstr(return_code); + + throw SqliteException{errmsg.str()}; } return reset(); } @@ -121,7 +128,11 @@ bool SqliteStatementWrapper::step() } else if (return_code == SQLITE_DONE) { return false; } else { - throw SqliteException("Error reading query result: " + std::to_string(return_code)); + std::stringstream errmsg; + errmsg << "Error reading SQL query. SQLite error (" << + return_code << "): " << sqlite3_errstr(return_code); + + throw SqliteException{errmsg.str()}; } } @@ -157,9 +168,12 @@ void SqliteStatementWrapper::obtain_column_value( void SqliteStatementWrapper::check_and_report_bind_error(int return_code) { if (return_code != SQLITE_OK) { - throw SqliteException("SQLite error when binding parameter " + - std::to_string(last_bound_parameter_index_) + ". Return code: " + - std::to_string(return_code)); + std::stringstream errmsg; + errmsg << "Error when binding SQL parameter " << + last_bound_parameter_index_ << ". SQLite error (" << + return_code << "): " << sqlite3_errstr(return_code); + + throw SqliteException{errmsg.str()}; } } diff --git a/rosbag2_storage_default_plugins/src/rosbag2_storage_default_plugins/sqlite/sqlite_wrapper.cpp b/rosbag2_storage_default_plugins/src/rosbag2_storage_default_plugins/sqlite/sqlite_wrapper.cpp index b3f69e2e09..c327026d1c 100644 --- a/rosbag2_storage_default_plugins/src/rosbag2_storage_default_plugins/sqlite/sqlite_wrapper.cpp +++ b/rosbag2_storage_default_plugins/src/rosbag2_storage_default_plugins/sqlite/sqlite_wrapper.cpp @@ -16,6 +16,7 @@ #include #include +#include #include #include @@ -37,7 +38,10 @@ SqliteWrapper::SqliteWrapper( int rc = sqlite3_open_v2(uri.c_str(), &db_ptr, SQLITE_OPEN_READONLY | SQLITE_OPEN_NOMUTEX, nullptr); if (rc != SQLITE_OK) { - throw SqliteException("Could not read-only open database. Error code: " + std::to_string(rc)); + std::stringstream errmsg; + errmsg << "Could not read-only open database. SQLite error (" << + rc << "): " << sqlite3_errstr(rc); + throw SqliteException{errmsg.str()}; } // throws an exception if the database is not valid. prepare_statement("PRAGMA schema_version;")->execute_and_reset(); @@ -45,12 +49,16 @@ SqliteWrapper::SqliteWrapper( int rc = sqlite3_open_v2(uri.c_str(), &db_ptr, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_NOMUTEX, nullptr); if (rc != SQLITE_OK) { - throw SqliteException( - "Could not read-write open database. Error code: " + std::to_string(rc)); + std::stringstream errmsg; + errmsg << "Could not read-write open database. SQLite error (" << + rc << "): " << sqlite3_errstr(rc); + throw SqliteException{errmsg.str()}; } prepare_statement("PRAGMA journal_mode = WAL;")->execute_and_reset(); prepare_statement("PRAGMA synchronous = NORMAL;")->execute_and_reset(); } + + sqlite3_extended_result_codes(db_ptr, 1); } SqliteWrapper::SqliteWrapper()