[glom/feature_backup2: 1/6] Spawn functions: Catch some exceptions.



commit 483c7ee1e6688a16827d797d9428b203722472b0
Author: Murray Cumming <murrayc murrayc com>
Date:   Thu Jul 1 10:54:07 2010 +0200

    Spawn functions: Catch some exceptions.
    
    	* glom/libglom/spawn_with_feedback.cc():
    	execute_command_line(), execute_command_line_and_wait(): Catch exceptions
    	so that these functions don't throw, simplifying caller code. For instance,
    	a command may be malformed and rejected by the shell.

 ChangeLog                           |    9 +++++++
 glom/libglom/spawn_with_feedback.cc |   46 ++++++++++++++++++++++++++++------
 2 files changed, 47 insertions(+), 8 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index cd1f714..c9e049b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2010-07-01  Murray Cumming  <murrayc murrayc com>
+
+	Spawn functions: Catch some exceptions.
+	
+	* glom/libglom/spawn_with_feedback.cc(): 
+	execute_command_line(), execute_command_line_and_wait(): Catch exceptions 
+	so that these functions don't throw, simplifying caller code. For instance, 
+	a command may be malformed and rejected by the shell.
+	
 2010-06-29  Murray Cumming  <murrayc murrayc com>
 
 	Improve stderr message.
diff --git a/glom/libglom/spawn_with_feedback.cc b/glom/libglom/spawn_with_feedback.cc
index 601c71f..475247d 100644
--- a/glom/libglom/spawn_with_feedback.cc
+++ b/glom/libglom/spawn_with_feedback.cc
@@ -74,7 +74,7 @@ static const unsigned int REDIRECT_STDERR = 2;
 class SpawnError: public std::runtime_error
 {
 public:
-  SpawnError(const std::string& error_message)
+  explicit SpawnError(const std::string& error_message)
   : std::runtime_error(error_message)
   {}
 };
@@ -243,7 +243,7 @@ public:
         redirect_to_string(child_stderr, stderr_text);
     }
 #ifdef GLIBMM_EXCEPTIONS_ENABLED
-    catch(Glib::Exception& ex)
+    catch(const Glib::Exception& ex)
     {
       throw SpawnError(ex.what());
     }
@@ -372,7 +372,7 @@ static int spawn_sync(const Glib::ustring& command_line, std::string* stdout_tex
 
   Glib::RefPtr<Glib::MainLoop> mainloop = Glib::MainLoop::create(false);
 
-  std::auto_ptr<const SpawnInfo> info = spawn_async(command_line, redirect_flags);
+  std::auto_ptr<const SpawnInfo> info = spawn_async(command_line, redirect_flags); //This could throw
   info->signal_finished().connect(
     sigc::bind(sigc::ptr_fun(&on_spawn_info_finished), sigc::ref(mainloop) ) );
 
@@ -391,8 +391,18 @@ bool execute_command_line_and_wait(const std::string& command, const SlotProgres
 {
   //Show UI progress feedback while we wait for the command to finish:
 
-  std::auto_ptr<const Impl::SpawnInfo> info = Impl::spawn_async(command, 0);
-
+  std::auto_ptr<const Impl::SpawnInfo> info;
+  
+  try
+  {
+    info = Impl::spawn_async(command, 0);
+  }
+  catch(const Impl::SpawnError& ex)
+  {
+    std::cerr << G_STRFUNC << ": exception: " << ex.what() << std::endl;
+    return false;
+  }
+  
   Glib::RefPtr<Glib::MainLoop> mainloop = Glib::MainLoop::create(false);
   info->signal_finished().connect(
     sigc::bind(sigc::ptr_fun(&on_spawn_info_finished), sigc::ref(mainloop) ) );
@@ -424,8 +434,18 @@ bool execute_command_line_and_wait(const std::string& command, const SlotProgres
 
   //Show UI progress feedback while we wait for the command to finish:
 
-  std::auto_ptr<const Impl::SpawnInfo> info = Impl::spawn_async(command, Impl::REDIRECT_STDOUT | Impl::REDIRECT_STDERR);
-
+  std::auto_ptr<const Impl::SpawnInfo> info;
+  
+  try
+  {
+    info = Impl::spawn_async(command, Impl::REDIRECT_STDOUT | Impl::REDIRECT_STDERR);
+  }
+  catch(const Impl::SpawnError& ex)
+  {
+    std::cerr << G_STRFUNC << ": exception: " << ex.what() << std::endl;
+    return false;
+  }
+  
   Glib::RefPtr<Glib::MainLoop> mainloop = Glib::MainLoop::create(false);
   info->signal_finished().connect(
     sigc::bind(sigc::ptr_fun(&on_spawn_info_finished), sigc::ref(mainloop) ) );
@@ -579,8 +599,18 @@ bool execute_command_line_and_wait_until_second_command_returns_success(const st
   std::cout << "debug: Command: " << command << std::endl;
   #endif //GLOM_SPAWN_DEBUG
 
-  std::auto_ptr<const Impl::SpawnInfo> info = Impl::spawn_async(command, Impl::REDIRECT_STDERR);
+  std::auto_ptr<const Impl::SpawnInfo> info;
 
+  try
+  {
+    info = Impl::spawn_async(command, Impl::REDIRECT_STDERR);
+  }
+  catch(const Impl::SpawnError& ex)
+  {
+    std::cerr << G_STRFUNC << ": exception: " << ex.what() << std::endl;
+    return false;
+  }
+  
   // While we wait for the second command to finish we
   // a) check whether the first command finished. If it did, and has a
   // negative error code, we assume it failed and return directly.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]