Simplified error handling in ./be
authorW. Trevor King <wking@drexel.edu>
Fri, 10 Jul 2009 18:11:23 +0000 (14:11 -0400)
committerW. Trevor King <wking@drexel.edu>
Fri, 10 Jul 2009 18:11:23 +0000 (14:11 -0400)
Removed superfluous nesting in ./be's error catching.  Also replaced
KeyErrors due to unknown commands with the more specific
cmdutil.UnknownCommand, since all sorts of programming errors can
raise KeyErrors.

Untested, since my working tree is a mess at the moment, but what
could go wrong? ;)

be
libbe/cmdutil.py

diff --git a/be b/be
index 2023daad692d16f9f768c1d6276a6612adb39a9f..b68a414ce7710622afb10e4a5c9e5db7b1e1cc28 100755 (executable)
--- a/be
+++ b/be
@@ -35,21 +35,21 @@ elif sys.argv[1] == '--version':
     print _version.version_info["revision_id"]
 else:
     try:
-        try:
-            sys.exit(cmdutil.execute(sys.argv[1], sys.argv[2:]))
-        except KeyError, e:
-            raise cmdutil.UserError("Unknown command \"%s\"" % e.args[0])
-        except cmdutil.GetHelp:
-            print cmdutil.help(sys.argv[1])
-            sys.exit(0)
-        except cmdutil.GetCompletions, e:
-            print '\n'.join(e.completions)
-            sys.exit(0)
-        except cmdutil.UsageError, e:
-            print "Invalid usage:", e
-            print "\nArgs:", sys.argv[1:]
-            print cmdutil.help(sys.argv[1])
-            sys.exit(1)
+        sys.exit(cmdutil.execute(sys.argv[1], sys.argv[2:]))
+    except cmdutil.GetHelp:
+        print cmdutil.help(sys.argv[1])
+        sys.exit(0)
+    except cmdutil.GetCompletions, e:
+        print '\n'.join(e.completions)
+        sys.exit(0)
+    except cmdutil.UnknownCommand, e:
+        print e
+        sys.exit(1)
+    except cmdutil.UsageError, e:
+        print "Invalid usage:", e
+        print "\nArgs:", sys.argv[1:]
+        print cmdutil.help(sys.argv[1])
+        sys.exit(1)
     except cmdutil.UserError, e:
         print "ERROR:"
         print e
index 0dd8ad0d7dd36980f575d790f784d3c462f371fd..7414e4677d725a1f16cb9ffc7e29af84a6759c04 100644 (file)
@@ -32,10 +32,10 @@ class UserError(Exception):
     def __init__(self, msg):
         Exception.__init__(self, msg)
 
-class UserErrorWrap(UserError):
-    def __init__(self, exception):
-        UserError.__init__(self, str(exception))
-        self.exception = exception
+class UnknownCommand(UserError):
+    def __init__(self, cmd):
+        Exception.__init__(self, "Unknown command '%s'" % cmd)
+        self.cmd = cmd
 
 class UsageError(Exception):
     pass
@@ -58,13 +58,13 @@ def get_command(command_name):
 
     >>> get_command("asdf")
     Traceback (most recent call last):
-    UserError: Unknown command asdf
+    UnknownCommand: Unknown command asdf
     >>> repr(get_command("list")).startswith("<module 'becommands.list' from ")
     True
     """
     cmd = plugin.get_plugin("becommands", command_name.replace("-", "_"))
     if cmd is None:
-        raise UserError("Unknown command %s" % command_name)
+        raise UnknownCommand(command_name)
     return cmd