diff options
| author | Mark Washenberger <mark.washenberger@rackspace.com> | 2012-03-02 15:57:55 -0500 |
|---|---|---|
| committer | Mark Washenberger <mark.washenberger@rackspace.com> | 2012-03-06 13:24:08 -0500 |
| commit | f56cef93ea6e3746a17152bcd1850ccf4b3dad3d (patch) | |
| tree | 3ae7a3c658ea392b5e1533a0eecd769743c74159 /nova/image | |
| parent | 0d78045e72efe7313ca54e726dd403793eb30b52 (diff) | |
Better glance exception handling
- Adds a conversion step that turns glance exceptions into nova
exceptions
- Converts Invalid to HTTPBadRequest in /images and /images/detail,
fixing bug 944846
- Makes stub glance client return glance exceptions instead of nova
exceptions
- Rebased off of http://review.openstack.org/4788 to pick up
MissingCredentialError handling as well, and added a test
- A few small, miscellaneous testing fixes for issues I noticed
Change-Id: I88eebfe7a7ac21cc5cd84ad84d64b311ddccf91e
Diffstat (limited to 'nova/image')
| -rw-r--r-- | nova/image/glance.py | 59 |
1 files changed, 48 insertions, 11 deletions
diff --git a/nova/image/glance.py b/nova/image/glance.py index 5edab2655..d924b2c41 100644 --- a/nova/image/glance.py +++ b/nova/image/glance.py @@ -23,6 +23,7 @@ import copy import datetime import json import random +import sys import time import urlparse @@ -202,7 +203,10 @@ class GlanceImageService(object): def _fetch_images(self, fetch_func, **kwargs): """Paginate through results from glance server""" - images = fetch_func(**kwargs) + try: + images = fetch_func(**kwargs) + except Exception: + _reraise_translated_exception() if not images: # break out of recursive loop to end pagination @@ -234,8 +238,8 @@ class GlanceImageService(object): try: image_meta = self._call_retry(context, 'get_image_meta', image_id) - except glance_exception.NotFound: - raise exception.ImageNotFound(image_id=image_id) + except Exception: + _reraise_translated_image_exception(image_id) if not self._is_image_available(context, image_meta): raise exception.ImageNotFound(image_id=image_id) @@ -256,8 +260,8 @@ class GlanceImageService(object): try: image_meta, image_chunks = self._call_retry(context, 'get_image', image_id) - except glance_exception.NotFound: - raise exception.ImageNotFound(image_id=image_id) + except Exception: + _reraise_translated_image_exception(image_id) for chunk in image_chunks: data.write(chunk) @@ -296,14 +300,11 @@ class GlanceImageService(object): # NOTE(vish): show is to check if image is available self.show(context, image_id) image_meta = self._translate_to_glance(image_meta) + client = self._get_client(context) try: - client = self._get_client(context) image_meta = client.update_image(image_id, image_meta, data) - except glance_exception.NotFound: - raise exception.ImageNotFound(image_id=image_id) - # NOTE(vish): this gets raised for public images - except glance_exception.MissingCredentialError: - raise exception.ImageNotAuthorized(image_id=image_id) + except Exception: + _reraise_translated_image_exception(image_id) base_image_meta = self._translate_from_glance(image_meta) return base_image_meta @@ -468,3 +469,39 @@ def _remove_read_only(image_meta): if attr in output: del output[attr] return output + + +def _reraise_translated_image_exception(image_id): + """Transform the exception for the image but keep its traceback intact.""" + exc_type, exc_value, exc_trace = sys.exc_info() + new_exc = _translate_image_exception(image_id, exc_type, exc_value) + raise new_exc, None, exc_trace + + +def _reraise_translated_exception(): + """Transform the exception but keep its traceback intact.""" + exc_type, exc_value, exc_trace = sys.exc_info() + new_exc = _translate_plain_exception(exc_type, exc_value) + raise new_exc, None, exc_trace + + +def _translate_image_exception(image_id, exc_type, exc_value): + if exc_type in (glance_exception.NotAuthorized, + glance_exception.MissingCredentialError): + return exception.ImageNotAuthorized(image_id=image_id) + if exc_type is glance_exception.NotFound: + return exception.ImageNotFound(image_id=image_id) + if exc_type is glance_exception.Invalid: + return exception.Invalid(exc_value) + return exc_value + + +def _translate_plain_exception(exc_type, exc_value): + if exc_type in (glance_exception.NotAuthorized, + glance_exception.MissingCredentialError): + return exception.NotAuthorized(exc_value) + if exc_type is glance_exception.NotFound: + return exception.NotFound(exc_value) + if exc_type is glance_exception.Invalid: + return exception.Invalid(exc_value) + return exc_value |
