Repository: FlorianLudwig/rueckenwind

Issue:
exceptions thrown in events
Exceptions thrown in events are caught and wrapped into an EventException (https://github.com/FlorianLudwig/rueckenwind/blob/master/rw/event.py#L75). As the EventException is not an HTTPError, it results in tornado not responding at all to the request.

Example:
```python

@plugin.init
def init():
    rw.httpbase.PRE_REQUEST.add(do_stuff)

@rw.gen.corouting
def do_stuff():
    raise HTTPError(401)
```

Expected behavior: the request is responded with a statuscode 401
Actual behavior: the request ist not responded at all

As there may be multiple exceptions in https://github.com/FlorianLudwig/rueckenwind/blob/master/rw/event.py#L75 common behavior would be to raise the first and log the rest.

Discussion:

Patch:
diff --git a/rw/event.py b/rw/event.py
index 103a14b..4cf69f3 100644
--- a/rw/event.py
+++ b/rw/event.py
@@ -20,22 +20,16 @@ Signal
 """
 from __future__ import absolute_import, division, print_function, with_statement
 
-import traceback
-
 from tornado import gen
-import rw.scope
-
-
-class EventException(Exception):
-    def __init__(self, exceptions):
-        self.exceptions = exceptions
-        message = '{} exceptions encountered:\n'.format(len(exceptions))
-        for func, e in exceptions:
-            message += '{}:\n{}'.format(func, e)
-        Exception.__init__(self, ''.join(message))
 
 
 class Event(set):
+    """
+    A simple within-process pub/sub event system.
+
+    If multiple callbacks are provided and raise exceptions,
+    the first detected exception is re-raised and all successive exceptions are ignored.
+    """
     def __init__(self, name, accumulator=None):
         super(Event, self).__init__()
         self.name = name
@@ -43,38 +37,24 @@ class Event(set):
 
     @gen.coroutine
     def __call__(self, *args, **kwargs):
-        scope = rw.scope.get_current_scope()
-        rw_tracing = scope.get('rw_trace', None) if scope else None
-
         re = []
-        exceptions = []
         futures = []
         for func in self:
-            try:
-                result = func(*args, **kwargs)
-                if isinstance(result, gen.Future):
-                    # we are not waiting for future objects result here
-                    # so they evaluate in parallel
-                    futures.append((func, result))
-                else:
-                    re.append(result)
-            except Exception:
-                exceptions.append((func, traceback.format_exc()))
+            result = func(*args, **kwargs)
+            if isinstance(result, gen.Future):
+                # we are not waiting for future objects result here
+                # so they evaluate in parallel
+                futures.append((func, result))
+            else:
+                re.append(result)
 
         # wait for results
         for func, future in futures:
-            try:
-                if not future.done():
-                    yield future
-                re.append(future.result())
-
-            except Exception:
-                exceptions.append((func, traceback.format_exc()))
-
-        if exceptions:
-            raise EventException(exceptions)
+            if not future.done():
+                yield future
+            re.append(future.result())
 
-        # apply accumolator
+        # apply accumulator
         if self.accumulator:
             re = self.accumulator(re)
 


Test patch:
diff --git a/test/test_event.py b/test/test_event.py
index 93abd6e..379487f 100644
--- a/test/test_event.py
+++ b/test/test_event.py
@@ -23,18 +23,9 @@ class MyTestCase(tornado.testing.AsyncTestCase):
         def fail():
             1 / 0
 
-        with pytest.raises(rw.event.EventException):
+        with pytest.raises(ZeroDivisionError):
             yield MY_EVENT()
 
-        try:
-            yield MY_EVENT()
-            assert False  # this line should never be reached
-        except rw.event.EventException as e:
-            # the original traceback should be get printed
-            assert 'in fail' in str(e)  # function name of the actual exception
-            assert '1 / 0' in str(e)  # the source line of the exception
-            assert 'ZeroDivisionError' in str(e)
-
     @tornado.testing.gen_test
     def test_event_listener(self):
         MY_EVENT = rw.event.Event('MY_EVENT')
@@ -87,14 +78,5 @@ class MyTestCase(tornado.testing.AsyncTestCase):
         MY_EVENT = rw.event.Event('MY_EVENT')
         MY_EVENT.add(something_lazy_failing)
 
-        with pytest.raises(rw.event.EventException):
-            yield MY_EVENT()
-
-        try:
+        with pytest.raises(ZeroDivisionError):
             yield MY_EVENT()
-            assert False  # this line should never be reached
-        except rw.event.EventException as e:
-            # the original traceback should be get printed
-            assert 'in something_lazy_failing' in str(e)  # function name of the actual exception
-            assert '1 / 0' in str(e)  # the source line of the exception
-            assert 'ZeroDivisionError' in str(e)
\ No newline at end of file