Skip to content

Commit e4d3ad1

Browse files
committed
Enhance SQLite error handling by throwing std::invalid_argument for invalid statement handles and adding new methods to retrieve database file path and statement count.
1 parent ee208f6 commit e4d3ad1

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

include/libp2p/storage/sqlite.hpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ namespace libp2p::storage {
6161
* @param st_handle - statement identifier
6262
* @param args - command arguments
6363
* @return number of rows affected, -1 in case of error
64+
* @throws std::invalid_argument if statement handle is invalid
6465
*/
6566
template <typename... Args>
6667
inline int execCommand(StatementHandle st_handle, const Args &...args) {
@@ -69,11 +70,14 @@ namespace libp2p::storage {
6970
bindArgs(st, args...);
7071
st.execute();
7172
return countChanges();
72-
} catch (const std::runtime_error &e) {
73+
} catch (const std::invalid_argument &e) {
7374
// getStatement can receive invalid handle
74-
log_->error(e.what());
75+
log_->error("Invalid statement handle: {}", e.what());
76+
throw; // Re-throw invalid_argument as it's a programming error
77+
} catch (const std::runtime_error &e) {
78+
log_->error("Runtime error during command execution: {}", e.what());
7579
} catch (...) {
76-
log_->error(getErrorMessage());
80+
log_->error("Unknown error during command execution: {}", getErrorMessage());
7781
}
7882
return -1;
7983
}
@@ -86,6 +90,7 @@ namespace libp2p::storage {
8690
* @param sink - query response consumer
8791
* @param args - query arguments
8892
* @return true when query was successfully executed, otherwise - false
93+
* @throws std::invalid_argument if statement handle is invalid
8994
*/
9095
template <typename Sink, typename... Args>
9196
inline bool execQuery(StatementHandle st_handle,
@@ -96,11 +101,14 @@ namespace libp2p::storage {
96101
bindArgs(st, args...);
97102
st >> sink;
98103
return true;
99-
} catch (const std::runtime_error &e) {
104+
} catch (const std::invalid_argument &e) {
100105
// getStatement can receive invalid handle
101-
log_->error(e.what());
106+
log_->error("Invalid statement handle: {}", e.what());
107+
throw; // Re-throw invalid_argument as it's a programming error
108+
} catch (const std::runtime_error &e) {
109+
log_->error("Runtime error during query execution: {}", e.what());
102110
} catch (...) {
103-
log_->error(getErrorMessage());
111+
log_->error("Unknown error during query execution: {}", getErrorMessage());
104112
}
105113
return false;
106114
}
@@ -121,6 +129,12 @@ namespace libp2p::storage {
121129
/// Returns the number of rows modified
122130
int countChanges() const;
123131

132+
/// Returns the database file path
133+
const std::string &getDatabaseFile() const;
134+
135+
/// Returns the number of prepared statements
136+
size_t getStatementCount() const;
137+
124138
::sqlite::database db_;
125139
std::string db_file_;
126140
log::Logger log_;

src/storage/sqlite.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,23 @@ namespace libp2p::storage {
4242
SQLite::database_binder &SQLite::getStatement(
4343
SQLite::StatementHandle handle) {
4444
if (handle >= statements_.size()) {
45-
throw std::runtime_error("SQLite: statement does not exist");
45+
throw std::invalid_argument("SQLite: statement handle " +
46+
std::to_string(handle) +
47+
" does not exist (max: " +
48+
std::to_string(statements_.size() - 1) + ")");
4649
}
4750
return statements_[handle];
4851
}
4952

5053
int SQLite::countChanges() const {
5154
return sqlite3_changes(db_.connection().get());
5255
}
56+
57+
const std::string &SQLite::getDatabaseFile() const {
58+
return db_file_;
59+
}
60+
61+
size_t SQLite::getStatementCount() const {
62+
return statements_.size();
63+
}
5364
} // namespace libp2p::storage

0 commit comments

Comments
 (0)