Often we [think we] know that all exceptions of a certain type should
be formatted as CommandErrors (i.e., the traceback is suppressed, and
the message is assumed intelligible). Rather than riddling .run() with
try...except blocks to do this, we can
@exception_to_command_error(ModelError)
def run(...)
which makes any ModelError into a CommandError in that samba-tool command.
Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Gary Lockyer <gary@catalyst.net.nz>
def __repr__(self):
return "CommandError(%s)" % self.message
+
+
+def exception_to_command_error(*exceptions):
+ """If you think all instances of a particular exceptions can be
+ turning to a CommandError, do this:
+
+ @exception_to_command_error(ValueError, LdbError):
+ def run(self, username, ...):
+ # continue as normal
+ """
+ def wrap2(f):
+ def wrap(*args, **kwargs):
+ try:
+ return f(*args, **kwargs)
+ except exceptions as e:
+ raise CommandError(e)
+ return wrap
+ return wrap2