Add exception handling in AppInstaller for exception safety 92/50892/3
authorTomasz Iwanek <t.iwanek@samsung.com>
Tue, 3 Nov 2015 08:13:31 +0000 (09:13 +0100)
committerPawel Sikorski <p.sikorski@samsung.com>
Thu, 5 Nov 2015 12:32:26 +0000 (04:32 -0800)
Although app-installer was designed not to throw exceptions
some code may still throw. This is some failure in design that
we have yet allowed exceptions to exist. Anyway,
rollback should work in cases where exception is thrown.

Change-Id: I0dc54154f7c3a29ce0b1f4dc9c0dda1a98cfd32f

src/common/app_installer.cc

index 4bab794..83e8433 100644 (file)
@@ -44,9 +44,15 @@ AppInstaller::Result AppInstaller::Run() {
   unsigned current_step = 1;
 
   for (; it != itEnd; ++it, ++current_step) {
-    process_status = (*it)->precheck();
-    if (process_status == Step::Status::OK)
-      process_status = (*it)->process();
+    try {
+      process_status = (*it)->precheck();
+      if (process_status == Step::Status::OK) {
+        process_status = (*it)->process();
+      }
+    } catch (const std::exception& err) {
+      LOG(ERROR) << "Exception occurred in process(): " << err.what();
+      process_status = Step::Status::ERROR;
+    }
 
     // send START signal as soon as possible
     if (pi_->state() == PkgmgrSignal::State::NOT_SENT) {
@@ -70,15 +76,26 @@ AppInstaller::Result AppInstaller::Run() {
   if (it != itEnd) {
     LOG(ERROR) << "Failure occurs";
     do {
-      if ((*it)->undo() != Step::Status::OK) {
-        LOG(ERROR) << "Error during undo operation, but continuing...";
+      try {
+        if ((*it)->undo() != Step::Status::OK) {
+          LOG(ERROR) << "Error during undo operation, but continuing...";
+          ret = Result::UNDO_ERROR;
+        }
+      } catch (const std::exception& err) {
+        LOG(ERROR) << "Exception occurred in undo(): " << err.what();
         ret = Result::UNDO_ERROR;
       }
     } while (it-- != itStart);
   } else {
     for (auto& step : steps_) {
-      if (step->clean() != Step::Status::OK) {
-        LOG(ERROR) << "Error during clean operation";
+      try {
+        if (step->clean() != Step::Status::OK) {
+          LOG(ERROR) << "Error during clean operation";
+          ret = Result::CLEANUP_ERROR;
+          break;
+        }
+      } catch (const std::exception& err) {
+        LOG(ERROR) << "Exception occurred in clean(): " << err.what();
         ret = Result::CLEANUP_ERROR;
         break;
       }