From ba2ac14279ed1f1115cc074950889636283b938c Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Sun, 10 Apr 2011 14:38:46 -0400 Subject: - An exception raised by a NewRequest event subscriber can now be caught by an exception view. --- CHANGES.txt | 3 +++ TODO.txt | 3 --- pyramid/router.py | 4 ++-- pyramid/tests/test_router.py | 21 +++++++++++++++++++++ 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 780df9e8a..c1a413642 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -30,6 +30,9 @@ Features - Added ``pyramid.i18n.make_localizer`` API (broken out from ``get_localizer`` guts). +- An exception raised by a NewRequest event subscriber can now be caught by + an exception view. + Bug Fixes --------- diff --git a/TODO.txt b/TODO.txt index 2cd208da2..d2d2a847a 100644 --- a/TODO.txt +++ b/TODO.txt @@ -6,9 +6,6 @@ Should-Have - MultiDict documentation. -- Move newrequest send event into inner try block in router so exceptions - generated by newrequest subscribers can serve as exception view gotos. - - https://github.com/Pylons/pyramid/issues#issue/67 (fixing would make it possible to render a static site from a static dir). diff --git a/pyramid/router.py b/pyramid/router.py index 089b5b280..b8a8639aa 100644 --- a/pyramid/router.py +++ b/pyramid/router.py @@ -71,10 +71,10 @@ class Router(object): threadlocals['request'] = request attrs = request.__dict__ attrs['registry'] = registry - has_listeners and registry.notify(NewRequest(request)) request_iface = IRequest - try: + try: # matches except Exception (exception view execution) + has_listeners and registry.notify(NewRequest(request)) # find the root object root_factory = self.root_factory if self.routes_mapper is not None: diff --git a/pyramid/tests/test_router.py b/pyramid/tests/test_router.py index 5871067bd..c559f58d0 100644 --- a/pyramid/tests/test_router.py +++ b/pyramid/tests/test_router.py @@ -477,6 +477,27 @@ class TestRouter(unittest.TestCase): self.assertEqual(response_events[0].response, response) self.assertEqual(result, response.app_iter) + def test_call_newrequest_evllist_exc_can_be_caught_by_exceptionview(self): + from pyramid.interfaces import INewRequest + from pyramid.interfaces import IExceptionViewClassifier + from pyramid.interfaces import IRequest + context = DummyContext() + self._registerTraverserFactory(context) + environ = self._makeEnviron() + def listener(event): + raise KeyError + self.registry.registerHandler(listener, (INewRequest,)) + exception_response = DummyResponse() + exception_response.app_iter = ["Hello, world"] + exception_view = DummyView(exception_response) + environ = self._makeEnviron() + self._registerView(exception_view, '', IExceptionViewClassifier, + IRequest, KeyError) + router = self._makeOne() + start_response = DummyStartResponse() + result = router(environ, start_response) + self.assertEqual(result, exception_response.app_iter) + def test_call_pushes_and_pops_threadlocal_manager(self): from pyramid.interfaces import IViewClassifier context = DummyContext() -- cgit v1.2.3