From 29dc47bd5045853d83a2343ec88c36ea89db188d Mon Sep 17 00:00:00 2001 From: Russell Bryant Date: Tue, 1 May 2012 17:31:26 -0400 Subject: Use save_and_reraise_exception() from common. This patch common.excutils from openstack-common, which includes save_and_reraise_exception(). The patch also converts the code base to use it from there instead of nova.utils and then removes it from nova.utils. The initial motivation for this was removing another nova dependency from nova.rpc so that it can eventually be moved to openstack-common. Change-Id: I7354ca51a02aec9c709cf33f77d4abc46acc2742 --- nova/utils.py | 33 ++++----------------------------- 1 file changed, 4 insertions(+), 29 deletions(-) (limited to 'nova/utils.py') diff --git a/nova/utils.py b/nova/utils.py index 71e734e8b..c57833c54 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -57,6 +57,7 @@ from nova import exception from nova import flags from nova import log as logging from nova.openstack.common import cfg +from nova.openstack.common import excutils from nova.openstack.common import importutils @@ -1257,32 +1258,6 @@ def generate_glance_url(): return "http://%s:%d" % (FLAGS.glance_host, FLAGS.glance_port) -@contextlib.contextmanager -def save_and_reraise_exception(): - """Save current exception, run some code and then re-raise. - - In some cases the exception context can be cleared, resulting in None - being attempted to be reraised after an exception handler is run. This - can happen when eventlet switches greenthreads or when running an - exception handler, code raises and catches an exception. In both - cases the exception context will be cleared. - - To work around this, we save the exception state, run handler code, and - then re-raise the original exception. If another exception occurs, the - saved exception is logged and the new exception is reraised. - """ - type_, value, traceback = sys.exc_info() - try: - yield - except Exception: - # NOTE(jkoelker): Using LOG.error here since it accepts exc_info - # as a kwargs. - LOG.error(_('Original exception being dropped'), - exc_info=(type_, value, traceback)) - raise - raise type_, value, traceback - - @contextlib.contextmanager def logging_error(message): """Catches exception, write message to the log, re-raise. @@ -1292,7 +1267,7 @@ def logging_error(message): try: yield except Exception as error: - with save_and_reraise_exception(): + with excutils.save_and_reraise_exception(): LOG.exception(message) @@ -1304,7 +1279,7 @@ def remove_path_on_error(path): try: yield except Exception: - with save_and_reraise_exception(): + with excutils.save_and_reraise_exception(): delete_if_exists(path) @@ -1671,7 +1646,7 @@ class UndoManager(object): .. note:: (sirp) This should only be called within an exception handler. """ - with save_and_reraise_exception(): + with excutils.save_and_reraise_exception(): if msg: LOG.exception(msg, **kwargs) -- cgit