Browse Source

Be more explicit if bottle route not found

Bastien Sevajol 6 years ago
parent
commit
529f7f5b8e
3 changed files with 24 additions and 7 deletions
  1. 6 0
      hapic/decorator.py
  2. 17 7
      hapic/doc.py
  3. 1 0
      hapic/hapic.py

+ 6 - 0
hapic/decorator.py View File

189
         self,
189
         self,
190
         token: str,
190
         token: str,
191
         description: ControllerDescription,
191
         description: ControllerDescription,
192
+        name: str='',
192
     ) -> None:
193
     ) -> None:
193
         self._token = token
194
         self._token = token
194
         self._description = description
195
         self._description = description
196
+        self._name = name
195
 
197
 
196
     @property
198
     @property
197
     def token(self) -> str:
199
     def token(self) -> str:
201
     def description(self) -> ControllerDescription:
203
     def description(self) -> ControllerDescription:
202
         return self._description
204
         return self._description
203
 
205
 
206
+    @property
207
+    def name(self) -> str:
208
+        return self._name
209
+
204
 
210
 
205
 class OutputBodyControllerWrapper(OutputControllerWrapper):
211
 class OutputBodyControllerWrapper(OutputControllerWrapper):
206
     pass
212
     pass

+ 17 - 7
hapic/doc.py View File

3
 import typing
3
 import typing
4
 
4
 
5
 import bottle
5
 import bottle
6
-from apispec import APISpec, Path
6
+from apispec import APISpec
7
+from apispec import Path
7
 from apispec.ext.marshmallow.swagger import schema2jsonschema
8
 from apispec.ext.marshmallow.swagger import schema2jsonschema
8
 
9
 
9
-from hapic.decorator import DecoratedController, DECORATION_ATTRIBUTE_NAME
10
+from hapic.decorator import DecoratedController
11
+from hapic.decorator import DECORATION_ATTRIBUTE_NAME
10
 
12
 
11
 # Bottle regular expression to locate url parameters
13
 # Bottle regular expression to locate url parameters
12
 from hapic.description import ControllerDescription
14
 from hapic.description import ControllerDescription
14
 BOTTLE_RE_PATH_URL = re.compile(r'<(?:[^:<>]+:)?([^<>]+)>')
16
 BOTTLE_RE_PATH_URL = re.compile(r'<(?:[^:<>]+:)?([^<>]+)>')
15
 
17
 
16
 
18
 
17
-def bottle_route_for_view(token, app):
19
+def bottle_route_for_view(
20
+    decorated_controller: DecoratedController,
21
+    app: bottle.Bottle,
22
+):
18
     for route in app.routes:
23
     for route in app.routes:
19
         route_token = getattr(
24
         route_token = getattr(
20
             route.callback,
25
             route.callback,
21
             DECORATION_ATTRIBUTE_NAME,
26
             DECORATION_ATTRIBUTE_NAME,
22
             None,
27
             None,
23
         )
28
         )
24
-        if route_token == token:
29
+        if route_token == decorated_controller.token:
25
             return route
30
             return route
26
-    # TODO: specialize exception
27
-    raise Exception('Not found')
31
+    # TODO BS 20171010: specialize exception
32
+    # TODO BS 20171010: Raise exception or print error ?
33
+    raise Exception(
34
+        'Decorated route "{}" was not found in bottle routes'.format(
35
+            decorated_controller.name,
36
+        )
37
+    )
28
 
38
 
29
 
39
 
30
 def bottle_generate_operations(
40
 def bottle_generate_operations(
143
         # with app.test_request_context():
153
         # with app.test_request_context():
144
         paths = {}
154
         paths = {}
145
         for controller in controllers:
155
         for controller in controllers:
146
-            bottle_route = bottle_route_for_view(controller.token, app)
156
+            bottle_route = bottle_route_for_view(controller, app)
147
             swagger_path = BOTTLE_RE_PATH_URL.sub(r'{\1}', bottle_route.rule)
157
             swagger_path = BOTTLE_RE_PATH_URL.sub(r'{\1}', bottle_route.rule)
148
 
158
 
149
             operations = bottle_generate_operations(
159
             operations = bottle_generate_operations(

+ 1 - 0
hapic/hapic.py View File

77
             decorated_controller = DecoratedController(
77
             decorated_controller = DecoratedController(
78
                 token=token,
78
                 token=token,
79
                 description=description,
79
                 description=description,
80
+                name=func.__name__,
80
             )
81
             )
81
             self._buffer.clear()
82
             self._buffer.clear()
82
             self._controllers.append(decorated_controller)
83
             self._controllers.append(decorated_controller)