Skip to content

Commit a9b2ed2

Browse files
committed
add a raise_ helper function which is slightly more convenient than six.reraise
1 parent bb4623d commit a9b2ed2

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

effect/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from __future__ import absolute_import
1111

12-
from ._base import Effect, perform, NoPerformerFoundError, catch
12+
from ._base import Effect, perform, NoPerformerFoundError, catch, raise_
1313
from ._sync import (
1414
NotSynchronousError,
1515
sync_perform,
@@ -28,5 +28,5 @@
2828
"Constant", "Error", "FirstError", "Func",
2929
"base_dispatcher",
3030
"TypeDispatcher", "ComposedDispatcher",
31-
"catch",
31+
"catch", "raise_",
3232
]

effect/_base.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def _perform(bouncer, effect):
167167

168168
def catch(exc_type, callable):
169169
"""
170-
A helper for handling errors of a specific type.
170+
A helper for handling errors of a specific type::
171171
172172
eff.on(error=catch(SpecificException,
173173
lambda exc_info: "got an error!"))
@@ -180,3 +180,17 @@ def catcher(exc_info):
180180
return callable(exc_info)
181181
six.reraise(*exc_info)
182182
return catcher
183+
184+
185+
def raise_(exception, tb=None):
186+
"""Simple convenience function to allow raising exceptions from lambdas.
187+
188+
This is slightly more convenient than ``six.reraise`` because it takes an
189+
exception instance instead of needing the type separate from the instance.
190+
191+
:param exception: An exception *instance* (not an exception type).
192+
193+
- ``raise_(exc)`` is the same as ``raise exc``.
194+
- ``raise_(exc, tb)`` is the same as ``raise type(exc), exc, tb``.
195+
"""
196+
six.reraise(type(exception), exception, tb)

0 commit comments

Comments
 (0)