Some exceptions raise UnicodeError

Use utils.exception_to_str to consistently cast the
exception to avoid the unicodeError stack trace.

Closes-Bug: #1340002
Related-Id: I54fce91c73adca846ec99ed50a57b95d16428ec1

Change-Id: I77478a8eeeefd85bf98138156429df53d0a4c079
Signed-off-by: Zhi Yan Liu <zhiyanl@cn.ibm.com>
This commit is contained in:
Zhi Yan Liu 2014-07-10 12:12:24 +08:00
parent 4829deeae1
commit d6286b3fd2
16 changed files with 59 additions and 44 deletions

View File

@ -541,7 +541,8 @@ class Controller(controller.BaseController):
request=req,
content_type="text/plain")
except exception.Invalid as e:
msg = _("Failed to reserve image. Got error: %(e)s") % {'e': e}
msg = (_("Failed to reserve image. Got error: %s") %
utils.exception_to_str(e))
for line in msg.split('\n'):
LOG.debug(line)
raise HTTPBadRequest(explanation=msg,
@ -575,7 +576,8 @@ class Controller(controller.BaseController):
copy_from)
except Exception as e:
upload_utils.safe_kill(req, image_meta['id'])
msg = "Copy from external source failed: %s" % e
msg = ("Copy from external source failed: %s" %
utils.exception_to_str(e))
LOG.debug(msg)
return
image_meta['size'] = image_size or image_meta['size']
@ -649,7 +651,8 @@ class Controller(controller.BaseController):
upload_utils.initiate_deletion(req, image_meta['location'],
image_id, CONF.delayed_delete)
except exception.Invalid as e:
msg = "Failed to activate image. Got error: %(e)s" % {'e': e}
msg = ("Failed to activate image. Got error: %s" %
utils.exception_to_str(e))
LOG.debug(msg)
raise HTTPBadRequest(explanation=msg,
request=req,
@ -939,21 +942,23 @@ class Controller(controller.BaseController):
image_data)
except exception.Invalid as e:
msg = ("Failed to update image metadata. Got error: %(e)s" %
{'e': e})
msg = ("Failed to update image metadata. Got error: %s" %
utils.exception_to_str(e))
LOG.debug(msg)
raise HTTPBadRequest(explanation=msg,
request=req,
content_type="text/plain")
except exception.NotFound as e:
msg = _("Failed to find image to update: %(e)s") % {'e': e}
msg = (_("Failed to find image to update: %s") %
utils.exception_to_str(e))
for line in msg.split('\n'):
LOG.info(line)
raise HTTPNotFound(explanation=msg,
request=req,
content_type="text/plain")
except exception.Forbidden as e:
msg = _("Forbidden to update image: %(e)s") % {'e': e}
msg = (_("Forbidden to update image: %s") %
utils.exception_to_str(e))
for line in msg.split('\n'):
LOG.info(line)
raise HTTPForbidden(explanation=msg,
@ -1037,14 +1042,16 @@ class Controller(controller.BaseController):
raise e
registry.delete_image_metadata(req.context, id)
except exception.NotFound as e:
msg = _("Failed to find image to delete: %(e)s") % {'e': e}
msg = (_("Failed to find image to delete: %s") %
utils.exception_to_str(e))
for line in msg.split('\n'):
LOG.info(line)
raise HTTPNotFound(explanation=msg,
request=req,
content_type="text/plain")
except exception.Forbidden as e:
msg = _("Forbidden to delete image: %(e)s") % {'e': e}
msg = (_("Forbidden to delete image: %s") %
utils.exception_to_str(e))
for line in msg.split('\n'):
LOG.info(line)
raise HTTPForbidden(explanation=msg,

View File

@ -182,7 +182,7 @@ def upload_data_to_store(req, image_meta, image_data, store, notifier):
content_type="text/plain")
except exception.StorageFull as e:
msg = _("Image storage media is full: %s") % e
msg = _("Image storage media is full: %s") % utils.exception_to_str(e)
LOG.error(msg)
safe_kill(req, image_id)
notifier.error('image.upload', msg)
@ -191,7 +191,8 @@ def upload_data_to_store(req, image_meta, image_data, store, notifier):
content_type='text/plain')
except exception.StorageWriteDenied as e:
msg = _("Insufficient permissions on image storage media: %s") % e
msg = (_("Insufficient permissions on image storage media: %s") %
utils.exception_to_str(e))
LOG.error(msg)
safe_kill(req, image_id)
notifier.error('image.upload', msg)
@ -211,7 +212,7 @@ def upload_data_to_store(req, image_meta, image_data, store, notifier):
except exception.StorageQuotaFull as e:
msg = (_("Denying attempt to upload image because it exceeds the ."
"quota: %s") % e)
"quota: %s") % utils.exception_to_str(e))
LOG.info(msg)
safe_kill(req, image_id)
notifier.error('image.upload', msg)

View File

@ -131,7 +131,7 @@ class ImagesController(object):
raise webob.exc.HTTPBadRequest(explanation=e.msg)
except exception.StorageQuotaFull as e:
msg = (_("Denying attempt to upload image because it exceeds the ."
"quota: %s") % e)
"quota: %s") % utils.exception_to_str(e))
LOG.info(msg)
raise webob.exc.HTTPRequestEntityTooLarge(
explanation=msg, request=req, content_type='text/plain')

View File

@ -42,6 +42,7 @@ from oslo.config import cfg
from glance.common import config
from glance.common import exception
from glance.common import utils
from glance.db import migration as db_migration
from glance.db.sqlalchemy import api as db_api
from glance.openstack.common.db.sqlalchemy import migration
@ -269,7 +270,7 @@ def main():
for arg in CONF.command.action_args]
return CONF.command.action_fn(*func_args, **func_kwargs)
except exception.GlanceException as e:
sys.exit("ERROR: %s" % e)
sys.exit("ERROR: %s" % utils.exception_to_str(e))
if __name__ == '__main__':

View File

@ -761,8 +761,6 @@ def main():
except ValueError as e:
logging.error(command.__doc__ % {'prog': command.__name__})
sys.exit("ERROR: %s" % e)
except Exception as e:
raise
if __name__ == '__main__':

View File

@ -259,13 +259,13 @@ class ImageCache(object):
with excutils.save_and_reraise_exception():
# image_iter has given us bad, (size_checked_iter has found a
# bad length), or corrupt data (checksum is wrong).
LOG.exception(e)
LOG.exception(utils.exception_to_str(e))
except Exception as e:
LOG.exception(_("Exception encountered while tee'ing "
"image '%(image_id)s' into cache: %(error)s. "
"Continuing with response.") %
{'image_id': image_id,
'error': e})
'error': utils.exception_to_str(e)})
# If no checksum provided continue responding even if
# caching failed.

View File

@ -192,18 +192,19 @@ class ImageProxy(glance.domain.proxy.Image):
try:
self.image.set_data(data, size)
except exception.StorageFull as e:
msg = (_("Image storage media is full: %s") % e)
msg = (_("Image storage media is full: %s") %
utils.exception_to_str(e))
self.notifier.error('image.upload', msg)
raise webob.exc.HTTPRequestEntityTooLarge(explanation=msg)
except exception.StorageWriteDenied as e:
msg = (_("Insufficient permissions on image storage media: %s")
% e)
% utils.exception_to_str(e))
self.notifier.error('image.upload', msg)
raise webob.exc.HTTPServiceUnavailable(explanation=msg)
except ValueError as e:
msg = (_("Cannot save data for image %(image_id)s: %(error)s") %
{'image_id': self.image.image_id,
'error': e})
'error': utils.exception_to_str(e)})
self.notifier.error('image.upload', msg)
raise webob.exc.HTTPBadRequest(explanation=
utils.exception_to_str(e))
@ -211,20 +212,20 @@ class ImageProxy(glance.domain.proxy.Image):
msg = (_("Unable to upload duplicate image data for image"
"%(image_id)s: %(error)s") %
{'image_id': self.image.image_id,
'error': e})
'error': utils.exception_to_str(e)})
self.notifier.error('image.upload', msg)
raise webob.exc.HTTPConflict(explanation=msg)
except exception.Forbidden as e:
msg = (_("Not allowed to upload image data for image %(image_id)s:"
" %(error)s") % {'image_id': self.image.image_id,
'error': e})
'error': utils.exception_to_str(e)})
self.notifier.error('image.upload', msg)
raise webob.exc.HTTPForbidden(explanation=msg)
except exception.NotFound as e:
msg = (_("Image %(image_id)s could not be found after upload."
" The image may have been deleted during the upload:"
" %(error)s") % {'image_id': self.image.image_id,
'error': e})
'error': utils.exception_to_str(e)})
self.notifier.error('image.upload', msg)
raise webob.exc.HTTPNotFound(explanation=utils.exception_to_str(e))
except webob.exc.HTTPError as e:
@ -232,14 +233,14 @@ class ImageProxy(glance.domain.proxy.Image):
msg = (_("Failed to upload image data for image %(image_id)s"
" due to HTTP error: %(error)s") %
{'image_id': self.image.image_id,
'error': e})
'error': utils.exception_to_str(e)})
self.notifier.error('image.upload', msg)
except Exception as e:
with excutils.save_and_reraise_exception():
msg = (_("Failed to upload image data for image %(image_id)s "
"due to internal error: %(error)s") %
{'image_id': self.image.image_id,
'error': e})
'error': utils.exception_to_str(e)})
self.notifier.error('image.upload', msg)
else:
payload = format_image_notification(self.image)

View File

@ -18,7 +18,6 @@ Reference implementation registry server WSGI controller
"""
from oslo.config import cfg
import six
from webob import exc
from glance.common import exception
@ -412,7 +411,7 @@ class Controller(object):
return exc.HTTPConflict(msg)
except exception.Invalid as e:
msg = (_("Failed to add image metadata. "
"Got error: %(e)s") % {'e': e})
"Got error: %s") % utils.exception_to_str(e))
LOG.error(msg)
return exc.HTTPBadRequest(msg)
except Exception:
@ -460,7 +459,7 @@ class Controller(object):
return dict(image=make_image_dict(updated_image))
except exception.Invalid as e:
msg = (_("Failed to update image metadata. "
"Got error: %(e)s") % {'e': e})
"Got error: %s") % utils.exception_to_str(e))
LOG.error(msg)
return exc.HTTPBadRequest(msg)
except exception.NotFound:
@ -482,7 +481,7 @@ class Controller(object):
request=req,
content_type='text/plain')
except exception.Conflict as e:
LOG.info(six.text_type(e))
LOG.info(utils.exception_to_str(e))
raise exc.HTTPConflict(body='Image operation conflicts',
request=req,
content_type='text/plain')

View File

@ -122,7 +122,8 @@ class Controller(object):
msg = _("Invalid membership association specified for "
"image %(id)s")
LOG.info(msg % {'id': image_id})
msg = _("Invalid membership association: %s") % e
msg = (_("Invalid membership association: %s") %
utils.exception_to_str(e))
raise webob.exc.HTTPBadRequest(explanation=msg)
add = []
@ -138,7 +139,8 @@ class Controller(object):
msg = _("Invalid membership association specified for "
"image %(id)s")
LOG.info(msg % {'id': image_id})
msg = _("Invalid membership association: %s") % e
msg = (_("Invalid membership association: %s") %
utils.exception_to_str(e))
raise webob.exc.HTTPBadRequest(explanation=msg)
# Figure out what can_share should be
@ -237,7 +239,8 @@ class Controller(object):
msg = _("Invalid membership association specified for "
"image %(id)s")
LOG.info(msg % {'id': image_id})
msg = _("Invalid membership association: %s") % e
msg = (_("Invalid membership association: %s") %
utils.exception_to_str(e))
raise webob.exc.HTTPBadRequest(explanation=msg)
# Look up an existing membership...

View File

@ -181,8 +181,8 @@ class ScrubFileQueue(ScrubQueue):
if image['status'] == 'deleted':
return
except exception.NotFound as e:
LOG.error(_("Failed to find image to delete: "
"%(e)s"), {'e': e})
LOG.error(_("Failed to find image to delete: %s"),
utils.exception_to_str(e))
return
delete_time = time.time() + self.scrub_time
@ -484,7 +484,7 @@ class Scrubber(object):
raise Exception(msg)
return atime
except Exception as e:
LOG.error(e)
LOG.error(utils.exception_to_str(e))
return None
def _update_cleanup_file(self, file_path, cleanup_time):

View File

@ -175,5 +175,5 @@ class Store(glance.store.base.Store):
raise exception.NotFound(reason)
except Exception as e:
LOG.exception(_("Failed to get image size due to "
"internal error: %s") % e)
"internal error: %s") % utils.exception_to_str(e))
return 0

View File

@ -464,4 +464,4 @@ class Store(glance.store.base.Store):
msg = _('Unable to remove partial image data for image %(id)s: '
'%(error)s')
LOG.error(msg % {'id': id,
'error': e})
'error': utils.exception_to_str(e)})

View File

@ -196,7 +196,8 @@ class Store(glance.store.base.Store):
self.user = str(CONF.rbd_store_user)
self.conf_file = str(CONF.rbd_store_ceph_conf)
except cfg.ConfigFileValueError as e:
reason = _("Error in store configuration: %s") % e
reason = (_("Error in store configuration: %s") %
utils.exception_to_str(e))
LOG.error(reason)
raise exception.BadStoreConfiguration(store_name='rbd',
reason=reason)

View File

@ -682,7 +682,8 @@ def create_bucket_if_missing(bucket, s3_conn):
s3_conn.create_bucket(bucket, location=location)
except S3ResponseError as e:
msg = (_("Failed to add bucket to S3.\n"
"Got error from S3: %(e)s") % {'e': e})
"Got error from S3: %s") %
utils.exception_to_str(e))
raise glance.store.BackendException(msg)
else:
msg = (_("The bucket %(bucket)s does not exist in "

View File

@ -195,7 +195,8 @@ class Store(glance.store.base.Store):
self.addr = CONF.sheepdog_store_address.strip()
self.port = CONF.sheepdog_store_port
except cfg.ConfigFileValueError as e:
reason = _("Error in store configuration: %s") % e
reason = (_("Error in store configuration: %s") %
utils.exception_to_str(e))
LOG.error(reason)
raise exception.BadStoreConfiguration(store_name='sheepdog',
reason=reason)
@ -211,7 +212,8 @@ class Store(glance.store.base.Store):
cmd = ["collie", "vdi", "list", "-a", self.addr, "-p", self.port]
processutils.execute(*cmd)
except Exception as e:
reason = _("Error in store configuration: %s") % e
reason = (_("Error in store configuration: %s") %
utils.exception_to_str(e))
LOG.error(reason)
raise exception.BadStoreConfiguration(store_name='sheepdog',
reason=reason)

View File

@ -123,7 +123,8 @@ def swift_retry_iter(resp_iter, length, store, location):
yield chunk
bytes_read += len(chunk)
except swiftclient.ClientException as e:
LOG.warn(_("Swift exception raised %s") % e)
LOG.warn(_("Swift exception raised %s") %
utils.exception_to_str(e))
if bytes_read != length:
if retries == CONF.swift_store_retry_get_count:
@ -559,7 +560,7 @@ class BaseStore(glance.store.base.Store):
raise exception.Duplicate(_("Swift already has an image at "
"this location"))
msg = (_("Failed to add object to Swift.\n"
"Got error from Swift: %(e)s") % {'e': e})
"Got error from Swift: %s") % utils.exception_to_str(e))
LOG.error(msg)
raise glance.store.BackendException(msg)