Don't crash when Command._run() raises an uncaught Exception.
authorW. Trevor King <wking@drexel.edu>
Mon, 10 May 2010 15:35:03 +0000 (11:35 -0400)
committerW. Trevor King <wking@drexel.edu>
Mon, 10 May 2010 15:35:03 +0000 (11:35 -0400)
Just print the traceback and move on.  This gives the user time to
exit gracefully (saving playlists etc.).

hooke/command.py

index 60e7233f6e70a2db34aa780ea8069cf0e2f67c1f..adb38185aa9c363d89935cc3e44987023961a4f9 100644 (file)
@@ -3,7 +3,9 @@
 """
 
 import Queue as queue
+import sys
 import textwrap
+import traceback
 
 
 class CommandExit (Exception):
@@ -16,6 +18,13 @@ class Success (CommandExit):
 class Failure (CommandExit):
     pass
 
+class UncaughtException (Failure):
+    def __init__(self, exception):
+        super(UncaughtException, self).__init__(exception)
+        self.exception = exception
+        self.exc_string = traceback.format_exc()
+        sys.exc_clear()
+
 class Command (object):
     """One-line command description here.
 
@@ -62,6 +71,11 @@ class Command (object):
                 outqueue.put(str(e))
                 outqueue.put(e)
                 return 1
+        except Exception, e:
+            x = UncaughtException(e)
+            outqueue.put(x.exc_string)
+            outqueue.put(x)
+            return 1
         outqueue.put(e)
         return 0