add some error handling to App
This commit is contained in:
parent
c842a1bfce
commit
276e8a4694
27
cliff/app.py
27
cliff/app.py
|
@ -59,6 +59,12 @@ class App(object):
|
|||
callback=self.show_verbose_help,
|
||||
help="show verbose help message and exit",
|
||||
)
|
||||
parser.add_option(
|
||||
'--debug',
|
||||
default=False,
|
||||
action='store_true',
|
||||
help='show tracebacks on errors',
|
||||
)
|
||||
return parser
|
||||
|
||||
def show_verbose_help(self, *args):
|
||||
|
@ -106,7 +112,7 @@ class App(object):
|
|||
"""
|
||||
return
|
||||
|
||||
def clean_up(self, cmd, result):
|
||||
def clean_up(self, cmd, result, err):
|
||||
"""Hook run after a command is done to shutdown the app.
|
||||
"""
|
||||
return
|
||||
|
@ -120,9 +126,18 @@ class App(object):
|
|||
self.configure_logging()
|
||||
cmd_factory, cmd_name, sub_argv = self.command_manager.find_command(remainder)
|
||||
cmd = cmd_factory(self, self.options)
|
||||
self.prepare_to_run_command(cmd)
|
||||
cmd_parser = cmd.get_parser(' '.join([self.NAME, cmd_name]))
|
||||
parsed_args = cmd_parser.parse_args(sub_argv)
|
||||
result = cmd.run(parsed_args)
|
||||
self.clean_up(cmd, result)
|
||||
err = None
|
||||
result = 1
|
||||
try:
|
||||
self.prepare_to_run_command(cmd)
|
||||
cmd_parser = cmd.get_parser(' '.join([self.NAME, cmd_name]))
|
||||
parsed_args = cmd_parser.parse_args(sub_argv)
|
||||
result = cmd.run(parsed_args)
|
||||
except Exception as err:
|
||||
if self.options.debug:
|
||||
LOG.exception(err)
|
||||
raise
|
||||
LOG.error(err)
|
||||
finally:
|
||||
self.clean_up(cmd, result, err)
|
||||
return result
|
||||
|
|
|
@ -19,14 +19,16 @@ class DemoApp(App):
|
|||
def prepare_to_run_command(self, cmd):
|
||||
self.log.debug('prepare_to_run_command %s', cmd.__class__.__name__)
|
||||
|
||||
def clean_up(self, cmd, result):
|
||||
def clean_up(self, cmd, result, err):
|
||||
self.log.debug('clean_up %s', cmd.__class__.__name__)
|
||||
if err:
|
||||
self.log.debug('got an error: %s', err)
|
||||
|
||||
|
||||
def main(argv=sys.argv[1:]):
|
||||
myapp = DemoApp()
|
||||
myapp.run(argv)
|
||||
return myapp.run(argv)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(sys.argv[1:])
|
||||
sys.exit(main(sys.argv[1:]))
|
||||
|
|
|
@ -12,3 +12,13 @@ class Simple(Command):
|
|||
self.log.info('sending greeting')
|
||||
self.log.debug('debugging')
|
||||
print('hi!')
|
||||
|
||||
|
||||
class Error(Command):
|
||||
"Always raises an error"
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
def run(self, parsed_args):
|
||||
self.log.info('causing error')
|
||||
raise RuntimeError('this is the expected exception')
|
||||
|
|
|
@ -163,6 +163,7 @@ setup(
|
|||
'cliff.demo': [
|
||||
'simple = cliffdemo.simple:Simple',
|
||||
'two_part = cliffdemo.simple:Simple',
|
||||
'error = cliffdemo.simple:Error',
|
||||
],
|
||||
# 'virtualenvwrapper.initialize': [
|
||||
# 'user_scripts = virtualenvwrapper.user_scripts:initialize',
|
||||
|
|
Loading…
Reference in New Issue