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

+ 17 - 7
hapic/doc.py View File

@@ -3,10 +3,12 @@ import re
3 3
 import typing
4 4
 
5 5
 import bottle
6
-from apispec import APISpec, Path
6
+from apispec import APISpec
7
+from apispec import Path
7 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 13
 # Bottle regular expression to locate url parameters
12 14
 from hapic.description import ControllerDescription
@@ -14,17 +16,25 @@ from hapic.description import ControllerDescription
14 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 23
     for route in app.routes:
19 24
         route_token = getattr(
20 25
             route.callback,
21 26
             DECORATION_ATTRIBUTE_NAME,
22 27
             None,
23 28
         )
24
-        if route_token == token:
29
+        if route_token == decorated_controller.token:
25 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 40
 def bottle_generate_operations(
@@ -143,7 +153,7 @@ class DocGenerator(object):
143 153
         # with app.test_request_context():
144 154
         paths = {}
145 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 157
             swagger_path = BOTTLE_RE_PATH_URL.sub(r'{\1}', bottle_route.rule)
148 158
 
149 159
             operations = bottle_generate_operations(

+ 1 - 0
hapic/hapic.py View File

@@ -77,6 +77,7 @@ class Hapic(object):
77 77
             decorated_controller = DecoratedController(
78 78
                 token=token,
79 79
                 description=description,
80
+                name=func.__name__,
80 81
             )
81 82
             self._buffer.clear()
82 83
             self._controllers.append(decorated_controller)