Skip to content

Commit 0e600d0

Browse files
committed
feat: Better error handling
1 parent 457ccd6 commit 0e600d0

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

src/database/sqlitedatabase.cpp

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Nickvision::Database
1010
m_isUnlocked{ true },
1111
m_database{ nullptr }
1212
{
13-
if (sqlite3_open_v2(m_path.string().c_str(), &m_database, m_flags, nullptr) != SQLITE_OK)
13+
if(sqlite3_open_v2(m_path.string().c_str(), &m_database, m_flags, nullptr) != SQLITE_OK)
1414
{
1515
throw std::runtime_error("Unable to open sql database.");
1616
}
@@ -124,11 +124,23 @@ namespace Nickvision::Database
124124
//Create temp encrypted database
125125
std::filesystem::path tempPath{ (m_path.string() + ".encrypt") };
126126
std::string cmd{ "ATTACH DATABASE '" + tempPath.string() + "' AS encrypted KEY '" + password + "'" };
127-
sqlite3_exec(m_database, cmd.c_str(), nullptr, nullptr, nullptr);
128-
sqlite3_exec(m_database, "SELECT sqlcipher_export('encrypted')", nullptr, nullptr, nullptr);
129-
sqlite3_exec(m_database, "DETACH DATABASE encrypted", nullptr, nullptr, nullptr);
127+
if(sqlite3_exec(m_database, cmd.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK)
128+
{
129+
throw std::runtime_error("Unable to attach temporary database.");
130+
}
131+
if(sqlite3_exec(m_database, "SELECT sqlcipher_export('encrypted')", nullptr, nullptr, nullptr) != SQLITE_OK)
132+
{
133+
throw std::runtime_error("Unable to encrypt temporary database.");
134+
}
135+
if(sqlite3_exec(m_database, "DETACH DATABASE encrypted", nullptr, nullptr, nullptr) != SQLITE_OK)
136+
{
137+
throw std::runtime_error("Unable to detach temporary database.");
138+
}
130139
//Remove old encrypted database
131-
sqlite3_close(m_database);
140+
if(sqlite3_close(m_database) != SQLITE_OK)
141+
{
142+
throw std::runtime_error("Unable to close old sql databse.");
143+
}
132144
std::filesystem::remove(m_path);
133145
std::filesystem::rename(tempPath, m_path);
134146
//Open new encrypted database
@@ -155,11 +167,23 @@ namespace Nickvision::Database
155167
//Create temporary decrypted database
156168
std::filesystem::path tempPath{ (m_path.string() + ".decrypt") };
157169
std::string cmd{ "ATTACH DATABASE '" + tempPath.string() + "' AS plaintext KEY ''" };
158-
sqlite3_exec(m_database, cmd.c_str(), nullptr, nullptr, nullptr);
159-
sqlite3_exec(m_database, "SELECT sqlcipher_export('plaintext')", nullptr, nullptr, nullptr);
160-
sqlite3_exec(m_database, "DETACH DATABASE plaintext", nullptr, nullptr, nullptr);
170+
if(sqlite3_exec(m_database, cmd.c_str(), nullptr, nullptr, nullptr) != SQLITE_OK)
171+
{
172+
throw std::runtime_error("Unable to attach temporary database.");
173+
}
174+
if(sqlite3_exec(m_database, "SELECT sqlcipher_export('plaintext')", nullptr, nullptr, nullptr) != SQLITE_OK)
175+
{
176+
throw std::runtime_error("Unable to decrypt temporary database.");
177+
}
178+
if(sqlite3_exec(m_database, "DETACH DATABASE plaintext", nullptr, nullptr, nullptr) != SQLITE_OK)
179+
{
180+
throw std::runtime_error("Unable to detach temporary database.");
181+
}
161182
//Remove old encrypted database
162-
sqlite3_close(m_database);
183+
if(sqlite3_close(m_database) != SQLITE_OK)
184+
{
185+
throw std::runtime_error("Unable to close old sql databse.");
186+
}
163187
std::filesystem::remove(m_path);
164188
std::filesystem::rename(tempPath, m_path);
165189
//Open new decrypted database

0 commit comments

Comments
 (0)