Add a return value to exceptions.

Returning the exception value doesn't work because a shell return value
is in the range of 0-255.

The default return value is 1 which means "something went wrong." The only
specific return value implemented so far is 2 which is "not found".
This commit is contained in:
Rob Crittenden
2009-06-09 15:24:23 -04:00
parent 0e29dd7226
commit fe84ffd0f1
2 changed files with 13 additions and 4 deletions

View File

@@ -36,7 +36,7 @@ import frontend
import backend
import plugable
import util
from errors import PublicError, CommandError, HelpError, InternalError, NoSuchNamespaceError, ValidationError
from errors import PublicError, CommandError, HelpError, InternalError, NoSuchNamespaceError, ValidationError, NotFound
from constants import CLI_TAB
from parameters import Password, Bytes
from request import ugettext as _
@@ -440,6 +440,9 @@ class textui(backend.Backend):
return -1
counter = len(entries)
if counter == 0:
raise NotFound(reason="No matching entries found")
i = 1
for e in entries:
# There is no guarantee that all attrs are in any given
@@ -690,7 +693,11 @@ class cli(backend.Executioner):
if param.password and param.name in kw:
del kw[param.name]
(args, options) = cmd.params_2_args_options(**kw)
cmd.output_for_cli(self.api.Backend.textui, result, *args, **options)
rv = cmd.output_for_cli(self.api.Backend.textui, result, *args, **options)
if rv:
return rv
else:
return 0
finally:
self.destroy_context()
@@ -799,7 +806,7 @@ def run(api):
api.register(klass)
api.load_plugins()
api.finalize()
api.Backend.cli.run(argv)
sys.exit(api.Backend.cli.run(argv))
except KeyboardInterrupt:
print ''
api.log.info('operation aborted')
@@ -811,4 +818,4 @@ def run(api):
if error is not None:
assert isinstance(error, PublicError)
api.log.error(error.strerror)
sys.exit(error.errno)
sys.exit(error.rval)

View File

@@ -240,6 +240,7 @@ class PublicError(StandardError):
"""
errno = 900
rval = 1
format = None
def __init__(self, format=None, message=None, **kw):
@@ -748,6 +749,7 @@ class NotFound(ExecutionError):
"""
errno = 4001
rval = 2
format = _('%(reason)s')
class DuplicateEntry(ExecutionError):