Browse Source

Merge pull request #49 from algoo/feature/global_exception_handler_for_pyramid

Bastien Sevajol 6 years ago
parent
commit
176eb69b42
No account linked to committer's email
2 changed files with 54 additions and 1 deletions
  1. 18 1
      hapic/ext/pyramid/context.py
  2. 36 0
      tests/func/test_exception_handling.py

+ 18 - 1
hapic/ext/pyramid/context.py View File

@@ -190,7 +190,24 @@ class PyramidContext(BaseContext):
190 190
         exception_class: typing.Type[Exception],
191 191
         http_code: int,
192 192
     ) -> None:
193
-        raise NotImplementedError('TODO')
193
+        def factory_view_func(exception_class, http_code):
194
+            def view_func(exc, request):
195
+                # TODO BS 2018-05-04: How to be attentive to hierarchy ?
196
+                error_builder = self.get_default_error_builder()
197
+                error_body = error_builder.build_from_exception(exc)
198
+                return self.get_response(
199
+                    json.dumps(error_body),
200
+                    http_code
201
+                )
202
+            return view_func
203
+
204
+        self.configurator.add_view(
205
+            view=factory_view_func(
206
+                exception_class,
207
+                http_code,
208
+            ),
209
+            context=exception_class,
210
+        )
194 211
 
195 212
     def is_debug(self) -> bool:
196 213
         return self.debug

+ 36 - 0
tests/func/test_exception_handling.py View File

@@ -1,8 +1,10 @@
1 1
 # coding: utf-8
2 2
 import bottle
3
+from pyramid.config import Configurator
3 4
 from webtest import TestApp
4 5
 
5 6
 from hapic import Hapic
7
+from hapic.ext.pyramid import PyramidContext
6 8
 from tests.base import Base
7 9
 from tests.base import MyContext
8 10
 
@@ -19,9 +21,43 @@ class TestExceptionHandling(Base):
19 21
             raise ZeroDivisionError('An exception message')
20 22
 
21 23
         app.route('/my-view', method='GET', callback=my_view)
24
+        # FIXME - G.M - 17-05-2018 - Verify if:
25
+        # - other view that work/raise an other exception do not go
26
+        # into this code for handle this exceptions.
27
+        # - response come clearly from there, not from web framework:
28
+        #  Check not only http code, but also body.
22 29
         context.handle_exception(ZeroDivisionError, http_code=400)
23 30
 
24 31
         test_app = TestApp(app)
25 32
         response = test_app.get('/my-view', status='*')
26 33
 
27 34
         assert 400 == response.status_code
35
+
36
+    def test_func__catch_one_exception__ok__pyramid(self):
37
+        # TODO - G.M - 17-05-2018 - Move/refactor this test
38
+        # in order to have here only framework agnostic test
39
+        # and framework_specific
40
+        # test somewhere else.
41
+        hapic = Hapic()
42
+        configurator = Configurator(autocommit=True)
43
+        context = PyramidContext(configurator)
44
+        hapic.set_context(context)
45
+
46
+        def my_view(context, request):
47
+            raise ZeroDivisionError('An exception message')
48
+
49
+        configurator.add_route('my_view','/my-view', request_method='GET')
50
+        configurator.add_view(my_view, route_name='my_view', renderer='json')
51
+
52
+        # FIXME - G.M - 17-05-2018 - Verify if:
53
+        # - other view that work/raise an other exception do not go
54
+        # into this code for handle this exceptions.
55
+        # - response come clearly from there, not from web framework:
56
+        #  Check not only http code, but also body.
57
+        context.handle_exception(ZeroDivisionError, http_code=400)
58
+
59
+        app = configurator.make_wsgi_app()
60
+        test_app = TestApp(app)
61
+        response = test_app.get('/my-view', status='*')
62
+
63
+        assert 400 == response.status_code