def raise_with_cause(exc_cls, message, *args, **kwargs): """Helper to raise + chain exceptions (when able) and associate a *cause*. NOTE(harlowja): Since in py3.x exceptions can be chained (due to :pep:`3134`) we should try to raise the desired exception with the given *cause* (or extract a *cause* from the current stack if able) so that the exception formats nicely in old and new versions of python. Since py2.x does **not** support exception chaining (or formatting) our root exception class has a :py:meth:`~taskflow.exceptions.TaskFlowException.pformat` method that can be used to get *similar* information instead (and this function makes sure to retain the *cause* in that case as well so that the :py:meth:`~taskflow.exceptions.TaskFlowException.pformat` method shows them). :param exc_cls: the :py:class:`~taskflow.exceptions.TaskFlowException` class to raise. :param message: the text/str message that will be passed to the exceptions constructor as its first positional argument. :param args: any additional positional arguments to pass to the exceptions constructor. :param kwargs: any additional keyword arguments to pass to the exceptions constructor. """ if not issubclass(exc_cls, TaskFlowException): raise ValueError("Subclass of taskflow exception is required") excutils.raise_with_cause(exc_cls, message, *args, **kwargs)
def raise_with_cause(exc_cls, message, *args, **kwargs): """Helper to raise + chain exceptions (when able) and associate a *cause*. NOTE(harlowja): Since in py3.x exceptions can be chained (due to :pep:`3134`) we should try to raise the desired exception with the given *cause* (or extract a *cause* from the current stack if able) so that the exception formats nicely in old and new versions of python. Since py2.x does **not** support exception chaining (or formatting) our root exception class has a :py:meth:`~taskflow.exceptions.TaskFlowException.pformat` method that can be used to get *similar* information instead (and this function makes sure to retain the *cause* in that case as well so that the :py:meth:`~taskflow.exceptions.TaskFlowException.pformat` method shows them). :param exc_cls: the :py:class:`~taskflow.exceptions.TaskFlowException` class to raise. :param message: the text/str message that will be passed to the exceptions constructor as its first positional argument. :param args: any additional positional arguments to pass to the exceptions constructor. :param kwargs: any additional keyword arguments to pass to the exceptions constructor. """ if not issubclass(exc_cls, TaskFlowException): raise ValueError("Subclass of taskflow exception is required") excutils.raise_with_cause(exc_cls, message, *args, **kwargs)
def raise_with_cause(exc_cls, message, *args, **kwargs): """Helper to raise + chain exceptions (when able) and associate a *cause*. **For internal usage only.** NOTE(harlowja): Since in py3.x exceptions can be chained (due to :pep:`3134`) we should try to raise the desired exception with the given *cause*. :param exc_cls: the :py:class:`~tooz.ToozError` class to raise. :param message: the text/str message that will be passed to the exceptions constructor as its first positional argument. :param args: any additional positional arguments to pass to the exceptions constructor. :param kwargs: any additional keyword arguments to pass to the exceptions constructor. """ if not issubclass(exc_cls, tooz.ToozError): raise ValueError("Subclass of tooz error is required") excutils.raise_with_cause(exc_cls, message, *args, **kwargs)
def raises_chained(): try: raise Fail2("I have been broken") except Fail2: excutils.raise_with_cause(Fail1, "I was broken")
def raise_delivery_failure(exc): excutils.raise_with_cause(DeliveryFailure, encodeutils.exception_to_unicode(exc), cause=exc)
def raise_delivery_failure(exc): excutils.raise_with_cause(DeliveryFailure, encodeutils.exception_to_unicode(exc), cause=exc)
def raises_chained(): try: raise Fail2("I have been broken") except Fail2: excutils.raise_with_cause(Fail1, "I was broken")