Add exception handling for database operations
authorSangyoon Jang <jeremy.jang@samsung.com>
Thu, 13 Mar 2025 07:42:07 +0000 (16:42 +0900)
committer장상윤/Tizen Platform Lab(SR)/삼성전자 <jeremy.jang@samsung.com>
Thu, 13 Mar 2025 07:58:46 +0000 (16:58 +0900)
Signed-off-by: Sangyoon Jang <jeremy.jang@samsung.com>
src/action/sqlite_db.cc
src/pkgmgr_plugin_parser/sqlite_db.cc

index f55f9e374defa8d70fd42d4b43fbaa9f99f8fbcc..5617d95056a97a2c0695531f275f2a9b7f686933 100644 (file)
@@ -48,10 +48,16 @@ std::string SqliteDb::GetAction(const std::string& name) {
 std::string SqliteDb::Select(const std::string& name) {
   auto q = std::move(tizen_base::Database::Sql(kGetActionQuery)
       .Bind(name));
-  auto r = conn_.Exec(q);
-  if (!static_cast<bool>(r)) {
-    LOG(ERROR) << "Failed to execute select query: "
-        << static_cast<const char*>(r);
+  tizen_base::Database::Result r;
+  try {
+    r = conn_.Exec(q);
+    if (!static_cast<bool>(r)) {
+      LOG(ERROR) << "Failed to execute select query: "
+          << static_cast<const char*>(r);
+      return {};
+    }
+  } catch (const tizen_base::DbException& e) {
+    LOG(ERROR) << "Exception occured: " << e.what();
     return {};
   }
 
@@ -73,10 +79,16 @@ std::string SqliteDb::Select(const std::string& name) {
 
 std::vector<std::string> SqliteDb::Select() {
   auto q = tizen_base::Database::Sql(kListActionQuery);
-  auto r = conn_.Exec(q);
-  if (!static_cast<bool>(r)) {
-    LOG(ERROR) << "Failed to execute select query: "
-        << static_cast<const char*>(r);
+  tizen_base::Database::Result r;
+  try {
+    r = conn_.Exec(q);
+    if (!static_cast<bool>(r)) {
+      LOG(ERROR) << "Failed to execute select query: "
+          << static_cast<const char*>(r);
+      return {};
+    }
+  } catch (const tizen_base::DbException& e) {
+    LOG(ERROR) << "Exception occured: " << e.what();
     return {};
   }
 
index 91961034dcd2e9dee28b545f21c6ba4812d2d8c2..f54e0d2605dbbca9f86171fb3bc63be4266e5631 100644 (file)
@@ -52,10 +52,16 @@ bool SqliteDb::Install(const ActionSchema& schema) {
       .Bind(schema.GetPkgId())
       .Bind(schema.GetName())
       .Bind(schema.GetJsonStr()));
-  auto r = conn_.Exec(q);
-  if (!static_cast<bool>(r)) {
-    LOG(ERROR) << "Failed to insert action schema: " << schema.GetName()
-        << ", error: " << static_cast<const char*>(r);
+  tizen_base::Database::Result r;
+  try {
+    r = conn_.Exec(q);
+    if (!static_cast<bool>(r)) {
+      LOG(ERROR) << "Failed to insert action schema: " << schema.GetName()
+          << ", error: " << static_cast<const char*>(r);
+      return false;
+    }
+  } catch (const tizen_base::DbException& e) {
+    LOG(ERROR) << "Exception occurred: " << e.what();
     return false;
   }
 
@@ -66,10 +72,16 @@ bool SqliteDb::Update(const ActionSchema& schema) {
   // delete and insert
   auto q = std::move(tizen_base::Database::Sql(kDeleteQuery)
       .Bind(schema.GetPkgId()));
-  auto r = conn_.Exec(q);
-  if (!static_cast<bool>(r)) {
-    LOG(ERROR) << "Failed to delete action schema from DB: "
-        << schema.GetName() << ", error: " << static_cast<const char*>(r);
+  tizen_base::Database::Result r;
+  try {
+    r = conn_.Exec(q);
+    if (!static_cast<bool>(r)) {
+      LOG(ERROR) << "Failed to delete action schema from DB: "
+          << schema.GetName() << ", error: " << static_cast<const char*>(r);
+      return false;
+    }
+  } catch (const tizen_base::DbException& e) {
+    LOG(ERROR) << "Exception occurred: " << e.what();
     return false;
   }
 
@@ -78,10 +90,15 @@ bool SqliteDb::Update(const ActionSchema& schema) {
       .Bind(schema.GetName())
       .Bind(schema.GetJsonStr()));
 
-  r = conn_.Exec(q);
-  if (!static_cast<bool>(r)) {
-    LOG(ERROR) << "Failed to update action schema into DB: "
-        << schema.GetName() << ", error: " << static_cast<const char*>(r);
+  try {
+    r = conn_.Exec(q);
+    if (!static_cast<bool>(r)) {
+      LOG(ERROR) << "Failed to update action schema into DB: "
+          << schema.GetName() << ", error: " << static_cast<const char*>(r);
+      return false;
+    }
+  } catch (const tizen_base::DbException& e) {
+    LOG(ERROR) << "Exception occurred: " << e.what();
     return false;
   }
 
@@ -91,10 +108,16 @@ bool SqliteDb::Update(const ActionSchema& schema) {
 bool SqliteDb::Uninstall(const ActionSchema& schema) {
   auto q = std::move(tizen_base::Database::Sql(kDeleteQuery)
       .Bind(schema.GetPkgId()));
-  auto r = conn_.Exec(q);
-  if (!static_cast<bool>(r)) {
-    LOG(ERROR) << "Failed to delete action schema: " << schema.GetName()
-        << ", error: " << static_cast<const char*>(r);
+  tizen_base::Database::Result r;
+  try {
+    r = conn_.Exec(q);
+    if (!static_cast<bool>(r)) {
+      LOG(ERROR) << "Failed to delete action schema: " << schema.GetName()
+          << ", error: " << static_cast<const char*>(r);
+      return false;
+    }
+  } catch (const tizen_base::DbException& e) {
+    LOG(ERROR) << "Exception occurred: " << e.what();
     return false;
   }