Improve import related error handling

If there was a problem importing a library we would incorrectly raise
an unsupported version error:

  $ glance --os-image-api-version 1 image-list
  "1" is not a supported API version. Example values are "1" or "2".

We should change this to provide information on the failed import, eg:

  $ glance --os-image-api-version 1 image-list
  No module named badimport

We also now raise the full stacktrace in this case if '--debug' is passed
on the command line.

Change-Id: I1c687ae6c5da239090b0b7a4a855b3271a9076da
Related-bug: 1402632
This commit is contained in:
Stuart McLaren
2015-04-07 12:32:36 +00:00
parent 9fac2a557e
commit 1f89beb609
2 changed files with 80 additions and 12 deletions
+24 -9
View File
@@ -47,6 +47,8 @@ from keystoneclient import session
osprofiler_profiler = importutils.try_import("osprofiler.profiler")
_ = _i18n._
SUPPORTED_VERSIONS = [1, 2]
class OpenStackImagesShell(object):
@@ -291,12 +293,7 @@ class OpenStackImagesShell(object):
self.subcommands = {}
subparsers = parser.add_subparsers(metavar='<subcommand>')
try:
submodule = utils.import_versioned_module(version, 'shell')
except ImportError:
print('"%s" is not a supported API version. Example '
'values are "1" or "2".' % version)
utils.exit()
submodule = utils.import_versioned_module(version, 'shell')
self._find_actions(subparsers, submodule)
self._find_actions(subparsers, self)
@@ -602,14 +599,32 @@ class OpenStackImagesShell(object):
# build available subcommands based on version
try:
api_version = int(options.os_image_api_version or url_version or 1)
if api_version not in SUPPORTED_VERSIONS:
raise ValueError
except ValueError:
print("Invalid API version parameter")
utils.exit()
msg = ("Invalid API version parameter. "
"Supported values are %s" % SUPPORTED_VERSIONS)
utils.exit(msg=msg)
if api_version == 2:
self._cache_schemas(options)
subcommand_parser = self.get_subcommand_parser(api_version)
try:
subcommand_parser = self.get_subcommand_parser(api_version)
except ImportError as e:
if options.debug:
traceback.print_exc()
if not str(e):
# Add a generic import error message if the raised ImportError
# has none.
raise ImportError('Unable to import module. Re-run '
'with --debug for more info.')
raise
except Exception:
if options.debug:
traceback.print_exc()
raise
self.parser = subcommand_parser
# Handle top-level --help/-h before attempting to parse