|
20 | 20 | import logging |
21 | 21 | import os |
22 | 22 | import sys |
| 23 | +import traceback |
23 | 24 |
|
24 | 25 | from cliff import app |
25 | 26 | from cliff import command |
@@ -75,6 +76,9 @@ def __init__(self): |
75 | 76 | version=openstackclient.__version__, |
76 | 77 | command_manager=commandmanager.CommandManager('openstack.cli')) |
77 | 78 |
|
| 79 | + # Until we have command line arguments parsed, dump any stack traces |
| 80 | + self.dump_stack_trace = True |
| 81 | + |
78 | 82 | # This is instantiated in initialize_app() only when using |
79 | 83 | # password flow auth |
80 | 84 | self.auth_client = None |
@@ -111,6 +115,18 @@ def __init__(self): |
111 | 115 | help="show this help message and exit", |
112 | 116 | ) |
113 | 117 |
|
| 118 | + def run(self, argv): |
| 119 | + try: |
| 120 | + super(OpenStackShell, self).run(argv) |
| 121 | + except Exception as e: |
| 122 | + if not logging.getLogger('').handlers: |
| 123 | + logging.basicConfig() |
| 124 | + if self.dump_stack_trace: |
| 125 | + self.log.error(traceback.format_exc(e)) |
| 126 | + else: |
| 127 | + self.log.error('Exception raised: ' + str(e)) |
| 128 | + return 1 |
| 129 | + |
114 | 130 | def build_option_parser(self, description, version): |
115 | 131 | parser = super(OpenStackShell, self).build_option_parser( |
116 | 132 | description, |
@@ -365,8 +381,10 @@ def initialize_app(self, argv): |
365 | 381 | requests_log = logging.getLogger("requests") |
366 | 382 | if self.options.debug: |
367 | 383 | requests_log.setLevel(logging.DEBUG) |
| 384 | + self.dump_stack_trace = True |
368 | 385 | else: |
369 | 386 | requests_log.setLevel(logging.WARNING) |
| 387 | + self.dump_stack_trace = False |
370 | 388 |
|
371 | 389 | # Save default domain |
372 | 390 | self.default_domain = self.options.os_default_domain |
@@ -437,11 +455,7 @@ def interact(self): |
437 | 455 |
|
438 | 456 |
|
439 | 457 | def main(argv=sys.argv[1:]): |
440 | | - try: |
441 | | - return OpenStackShell().run(argv) |
442 | | - except Exception: |
443 | | - return 1 |
444 | | - |
| 458 | + return OpenStackShell().run(argv) |
445 | 459 |
|
446 | 460 | if __name__ == "__main__": |
447 | 461 | sys.exit(main(sys.argv[1:])) |
0 commit comments