diff --git a/novaclient/shell.py b/novaclient/shell.py index 93f7bbe0a..583cddb66 100644 --- a/novaclient/shell.py +++ b/novaclient/shell.py @@ -31,7 +31,6 @@ from keystoneclient import session as ksession from oslo_utils import encodeutils from oslo_utils import importutils from oslo_utils import strutils -import six HAS_KEYRING = False all_errors = ValueError @@ -897,12 +896,11 @@ def main(): try: argv = [encodeutils.safe_decode(a) for a in sys.argv[1:]] OpenStackComputeShell().main(argv) - - except Exception as e: - logger.debug(e, exc_info=1) - details = {'name': encodeutils.safe_encode(e.__class__.__name__), - 'msg': encodeutils.safe_encode(six.text_type(e))} - print("ERROR (%(name)s): %(msg)s" % details, + except Exception as exc: + logger.debug(exc, exc_info=1) + print("ERROR (%s): %s" + % (exc.__class__.__name__, + encodeutils.exception_to_unicode(exc)), file=sys.stderr) sys.exit(1) except KeyboardInterrupt: diff --git a/novaclient/tests/unit/test_shell.py b/novaclient/tests/unit/test_shell.py index 844306fc1..a2c86b6d0 100644 --- a/novaclient/tests/unit/test_shell.py +++ b/novaclient/tests/unit/test_shell.py @@ -521,6 +521,16 @@ class ShellTest(utils.TestCase): self.assertEqual(password, 'password') self.assertIs(client_kwargs['session'], None) + @mock.patch.object(novaclient.shell.OpenStackComputeShell, 'main') + def test_main_error_handling(self, mock_compute_shell): + class MyException(Exception): + pass + with mock.patch('sys.stderr', six.StringIO()): + mock_compute_shell.side_effect = MyException('message') + self.assertRaises(SystemExit, novaclient.shell.main) + err = sys.stderr.getvalue() + self.assertEqual(err, 'ERROR (MyException): message\n') + class TestLoadVersionedActions(utils.TestCase):