Refactor exception handling in cmd.api
Replace the known exception tuple with a map of exception classes to error codes to preserve backward compatibility of exit codes. Also change the code to handle unknown members of the Exception hierarchy without breaking. Co-authored-by: jiangpch <jiangpengcheng@navercorp.com> Co-authored-by: Brian Rosmaita <rosmaita.fossdev@gmail.com> Closes-Bug: #1726213 Change-Id: Iabfc2ded45a576a18bdb30a6c3ada8b9799a3196
This commit is contained in:
parent
fd16fa4f25
commit
7edf7ff66d
@ -56,16 +56,19 @@ CONF = cfg.CONF
|
||||
CONF.import_group("profiler", "glance.common.wsgi")
|
||||
logging.register_options(CONF)
|
||||
|
||||
KNOWN_EXCEPTIONS = (RuntimeError,
|
||||
exception.WorkerCreationFailure,
|
||||
glance_store.exceptions.BadStoreConfiguration,
|
||||
ValueError)
|
||||
# NOTE(rosmaita): Any new exceptions added should preserve the current
|
||||
# error codes for backward compatibility. The value 99 is returned
|
||||
# for errors not listed in this map.
|
||||
ERROR_CODE_MAP = {RuntimeError: 1,
|
||||
exception.WorkerCreationFailure: 2,
|
||||
glance_store.exceptions.BadStoreConfiguration: 3,
|
||||
ValueError: 4,
|
||||
cfg.ConfigFileValueError: 5}
|
||||
|
||||
|
||||
def fail(e):
|
||||
global KNOWN_EXCEPTIONS
|
||||
return_code = KNOWN_EXCEPTIONS.index(type(e)) + 1
|
||||
sys.stderr.write("ERROR: %s\n" % encodeutils.exception_to_unicode(e))
|
||||
return_code = ERROR_CODE_MAP.get(type(e), 99)
|
||||
sys.exit(return_code)
|
||||
|
||||
|
||||
@ -89,7 +92,7 @@ def main():
|
||||
server = wsgi.Server(initialize_glance_store=True)
|
||||
server.start(config.load_paste_app('glance-api'), default_port=9292)
|
||||
server.wait()
|
||||
except KNOWN_EXCEPTIONS as e:
|
||||
except Exception as e:
|
||||
fail(e)
|
||||
|
||||
|
||||
|
@ -132,3 +132,61 @@ class TestGlanceApiCmd(test_utils.BaseTestCase):
|
||||
self._raise(RuntimeError))
|
||||
exit = self.assertRaises(SystemExit, glance.cmd.cache_pruner.main)
|
||||
self.assertEqual('ERROR: ', exit.code)
|
||||
|
||||
def test_fail_with_value_error(self):
|
||||
with mock.patch('sys.stderr.write') as mock_stderr:
|
||||
with mock.patch('sys.exit') as mock_exit:
|
||||
exc_msg = 'A ValueError, LOL!'
|
||||
exc = ValueError(exc_msg)
|
||||
glance.cmd.api.fail(exc)
|
||||
mock_stderr.assert_called_once_with('ERROR: %s\n' % exc_msg)
|
||||
mock_exit.assert_called_once_with(4)
|
||||
|
||||
def test_fail_with_config_exception(self):
|
||||
with mock.patch('sys.stderr.write') as mock_stderr:
|
||||
with mock.patch('sys.exit') as mock_exit:
|
||||
exc_msg = 'A ConfigError by George!'
|
||||
exc = cfg.ConfigFileValueError(exc_msg)
|
||||
glance.cmd.api.fail(exc)
|
||||
mock_stderr.assert_called_once_with('ERROR: %s\n' % exc_msg)
|
||||
mock_exit.assert_called_once_with(5)
|
||||
|
||||
def test_fail_with_unknown_exception(self):
|
||||
with mock.patch('sys.stderr.write') as mock_stderr:
|
||||
with mock.patch('sys.exit') as mock_exit:
|
||||
exc_msg = 'A Crazy Unkown Error.'
|
||||
exc = CrayCray(exc_msg)
|
||||
glance.cmd.api.fail(exc)
|
||||
mock_stderr.assert_called_once_with('ERROR: %s\n' % exc_msg)
|
||||
mock_exit.assert_called_once_with(99)
|
||||
|
||||
def test_main_with_store_config_exception(self):
|
||||
with mock.patch.object(glance.common.config,
|
||||
'parse_args') as mock_config:
|
||||
with mock.patch('sys.exit') as mock_exit:
|
||||
exc = store.exceptions.BadStoreConfiguration()
|
||||
mock_config.side_effect = exc
|
||||
glance.cmd.api.main()
|
||||
mock_exit.assert_called_once_with(3)
|
||||
|
||||
def test_main_with_runtime_error(self):
|
||||
with mock.patch.object(glance.common.config,
|
||||
'parse_args') as mock_config:
|
||||
with mock.patch('sys.exit') as mock_exit:
|
||||
exc = RuntimeError()
|
||||
mock_config.side_effect = exc
|
||||
glance.cmd.api.main()
|
||||
mock_exit.assert_called_once_with(1)
|
||||
|
||||
def test_main_with_worker_creation_failure(self):
|
||||
with mock.patch.object(glance.common.config,
|
||||
'parse_args') as mock_config:
|
||||
with mock.patch('sys.exit') as mock_exit:
|
||||
exx = exc.WorkerCreationFailure()
|
||||
mock_config.side_effect = exx
|
||||
glance.cmd.api.main()
|
||||
mock_exit.assert_called_once_with(2)
|
||||
|
||||
|
||||
class CrayCray(Exception):
|
||||
pass
|
||||
|
Loading…
Reference in New Issue
Block a user