Merge "Use exception_filter from oslo_utils"

This commit is contained in:
Jenkins 2016-07-21 01:36:34 +00:00 committed by Gerrit Code Review
commit 49a4a75506
2 changed files with 5 additions and 64 deletions

View File

@ -12,14 +12,14 @@
# under the License.
import abc
import functools
import sys
import weakref
from keystoneauth1 import exceptions
from keystoneauth1.identity import generic
from keystoneauth1 import plugin
from oslo_config import cfg
from oslo_utils import excutils
import requests
import six
@ -29,63 +29,6 @@ from heat.common import exception as heat_exception
cfg.CONF.import_opt('client_retry_limit', 'heat.common.config')
class ExceptionFilter(object):
"""A context manager that prevents some exceptions from being raised.
For backwards compatibility, these objects can also be called with the
exception value as an argument - any non-matching exception will be
re-raised from this call. We attempt but cannot guarantee to keep the same
traceback; the context manager method is preferred for this reason except
in cases where the ignored exception affects control flow.
Use this class as a decorator for a function that returns whether a given
exception should be ignored. e.g.
>>> @ExceptionFilter
>>> def ignore_assertions(ex):
... return isinstance(ex, AssertionError)
and then use it as a context manager:
>>> with ignore_assertions:
... assert False
or call it:
>>> try:
... assert False
... except Exception as ex:
... ignore_assertions(ex)
"""
def __init__(self, should_ignore_ex):
self._should_ignore_ex = should_ignore_ex
functools.update_wrapper(self, should_ignore_ex)
def __get__(self, obj, owner):
return type(self)(six.create_bound_method(self._should_ignore_ex, obj))
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_val is not None:
return self._should_ignore_ex(exc_val)
def __call__(self, ex):
"""Re-raise any exception value not being filtered out.
If the exception was the last to be raised, it will be re-raised with
its original traceback.
"""
if not self._should_ignore_ex(ex):
exc_type, exc_val, traceback = sys.exc_info()
if exc_val is ex:
six.reraise(exc_type, exc_val, traceback)
else:
raise ex
@six.add_metaclass(abc.ABCMeta)
class ClientPlugin(object):
@ -217,12 +160,12 @@ class ClientPlugin(object):
"""Returns True if the exception is a conflict."""
return False
@ExceptionFilter
@excutils.exception_filter
def ignore_not_found(self, ex):
"""Raises the exception unless it is a not-found."""
return self.is_not_found(ex)
@ExceptionFilter
@excutils.exception_filter
def ignore_conflict_and_not_found(self, ex):
"""Raises the exception unless it is a conflict or not-found."""
return self.is_conflict(ex) or self.is_not_found(ex)

View File

@ -36,8 +36,6 @@ from heat.objects import stack as stack_object
from heat.objects import stack_lock
from heat.rpc import api as rpc_api
from heat.engine.clients.client_plugin import ExceptionFilter
LOG = logging.getLogger(__name__)
@ -343,7 +341,7 @@ class StackResource(resource.Resource):
DeprecationWarning)
return self.translate_remote_exceptions(ex)
@ExceptionFilter
@excutils.exception_filter
def translate_remote_exceptions(self, ex):
if (isinstance(ex, exception.ActionInProgress) and
self.stack.action == self.stack.ROLLBACK):