Browse Source

Merge pull request #77 from tracim/fix/59_global_exception_handling

Damien Accorsi 6 years ago
parent
commit
a0ad2044cd
No account linked to committer's email
4 changed files with 12 additions and 8 deletions
  1. 1 1
      setup.py
  2. 10 5
      tracim/__init__.py
  3. 1 0
      tracim/config.py
  4. 0 2
      tracim/tests/functional/test_session.py

+ 1 - 1
setup.py View File

@@ -24,7 +24,7 @@ requires = [
24 24
     'zope.sqlalchemy',
25 25
     'alembic',
26 26
     # API
27
-    'hapic',
27
+    'hapic>=0.41',
28 28
     'marshmallow <3.0.0a1,>2.0.0',
29 29
     # CLI
30 30
     'cliff',

+ 10 - 5
tracim/__init__.py View File

@@ -5,6 +5,8 @@ import time
5 5
 from pyramid.config import Configurator
6 6
 from pyramid.authentication import BasicAuthAuthenticationPolicy
7 7
 from hapic.ext.pyramid import PyramidContext
8
+from pyramid.exceptions import NotFound
9
+from sqlalchemy.exc import OperationalError
8 10
 
9 11
 from tracim.extensions import hapic
10 12
 from tracim.config import CFG
@@ -52,12 +54,15 @@ def web(global_config, **local_settings):
52 54
     # Add SqlAlchemy DB
53 55
     configurator.include('.models')
54 56
     # set Hapic
55
-    hapic.set_context(
56
-        PyramidContext(
57
-            configurator=configurator,
58
-            default_error_builder=ErrorSchema()
59
-        )
57
+    context = PyramidContext(
58
+        configurator=configurator,
59
+        default_error_builder=ErrorSchema(),
60
+        debug=app_config.DEBUG,
60 61
     )
62
+    hapic.set_context(context)
63
+    context.handle_exception(NotFound, 404)
64
+    context.handle_exception(OperationalError, 500)
65
+    context.handle_exception(Exception, 500)
61 66
     # Add controllers
62 67
     session_api = SessionController()
63 68
     configurator.include(session_api.bind, route_prefix=BASE_API_V2)

+ 1 - 0
tracim/config.py View File

@@ -128,6 +128,7 @@ class CFG(object):
128 128
             '604800',
129 129
         ))
130 130
 
131
+        self.DEBUG = asbool(settings.get('debug', False))
131 132
         # TODO - G.M - 27-03-2018 - [Email] Restore email config
132 133
         ###
133 134
         # EMAIL related stuff (notification, reply)

+ 0 - 2
tracim/tests/functional/test_session.py View File

@@ -17,8 +17,6 @@ class TestLogoutEndpoint(FunctionalTest):
17 17
 
18 18
 class TestLoginEndpointUnititedDB(FunctionalTestNoDB):
19 19
 
20
-    @pytest.mark.xfail(raises=OperationalError,
21
-                       reason='Not supported yet by hapic')
22 20
     def test_api__try_login_enpoint__err_500__no_inited_db(self):
23 21
         params = {
24 22
             'email': 'admin@admin.admin',