Browse Source

naming and comments

Bastien Sevajol 6 years ago
parent
commit
7a69d2c962
5 changed files with 41 additions and 12 deletions
  1. 23 2
      hapic/context.py
  2. 4 2
      hapic/ext/bottle/context.py
  3. 4 2
      hapic/ext/flask/context.py
  4. 4 2
      hapic/ext/pyramid/context.py
  5. 6 4
      tests/base.py

+ 23 - 2
hapic/context.py View File

@@ -153,13 +153,22 @@ class BaseContext(ContextInterface):
153 153
         self,
154 154
         func: typing.Callable[..., typing.Any],
155 155
     ) -> typing.Callable[..., typing.Any]:
156
+        """
157
+        Return a decorator who catch exceptions raised during given function
158
+        execution and return a response built by the default error builder.
159
+
160
+        :param func: decorated function
161
+        :return: the decorator
162
+        """
156 163
         def decorator(*args, **kwargs):
157 164
             try:
158 165
                 return func(*args, **kwargs)
159 166
             except Exception as exc:
160 167
                 # Reverse list to read first user given exception before
161 168
                 # the hapic default Exception catch
162
-                handled = reversed(self._get_handled_exception_classes())
169
+                handled = reversed(
170
+                    self._get_handled_exception_class_and_http_codes(),
171
+                )
163 172
                 for handled_exception_class, http_code in handled:
164 173
                     # TODO BS 2018-05-04: How to be attentive to hierarchy ?
165 174
                     if isinstance(exc, handled_exception_class):
@@ -173,9 +182,14 @@ class BaseContext(ContextInterface):
173 182
 
174 183
         return decorator
175 184
 
176
-    def _get_handled_exception_classes(
185
+    def _get_handled_exception_class_and_http_codes(
177 186
         self,
178 187
     ) -> typing.List[typing.Tuple[typing.Type[Exception], int]]:
188
+        """
189
+        :return: A list of tuple where: thirst item of tuple is a exception
190
+        class and second tuple item is a http code. This list will be used by
191
+        `handle_exceptions_decorator_builder` decorator to catch exceptions.
192
+        """
179 193
         raise NotImplementedError()
180 194
 
181 195
     def _add_exception_class_to_catch(
@@ -183,4 +197,11 @@ class BaseContext(ContextInterface):
183 197
         exception_class: typing.Type[Exception],
184 198
         http_code: int,
185 199
     ) -> None:
200
+        """
201
+        Add an exception class to catch and matching http code. Will be used by
202
+        `handle_exceptions_decorator_builder` decorator to catch exceptions.
203
+        :param exception_class: exception class to catch
204
+        :param http_code: http code to use if this exception catched
205
+        :return:
206
+        """
186 207
         raise NotImplementedError()

+ 4 - 2
hapic/ext/bottle/context.py View File

@@ -33,7 +33,7 @@ class BottleContext(BaseContext):
33 33
         app: bottle.Bottle,
34 34
         default_error_builder: ErrorBuilderInterface=None,
35 35
     ):
36
-        self._handled_exceptions = []  # type: typing.List[typing.Tuple[typing.Type[Exception], int]]  # nopep8
36
+        self._handled_exception_and_http_codes = []  # type: typing.List[typing.Tuple[typing.Type[Exception], int]]  # nopep8
37 37
         self.app = app
38 38
         self.default_error_builder = \
39 39
             default_error_builder or DefaultErrorBuilder()  # FDV
@@ -141,7 +141,9 @@ class BottleContext(BaseContext):
141 141
         exception_class: typing.Type[Exception],
142 142
         http_code: int,
143 143
     ) -> None:
144
-        self._handled_exceptions.append((exception_class, http_code))
144
+        self._handled_exception_and_http_codes.append(
145
+            (exception_class, http_code),
146
+        )
145 147
 
146 148
     def _install_exceptions_handler(self) -> None:
147 149
         self.app.install(self.handle_exceptions_decorator_builder)

+ 4 - 2
hapic/ext/flask/context.py View File

@@ -33,7 +33,7 @@ class FlaskContext(BaseContext):
33 33
         app: Flask,
34 34
         default_error_builder: ErrorBuilderInterface=None,
35 35
     ):
36
-        self._handled_exceptions = []  # type: typing.List[typing.Tuple[typing.Type[Exception], int]]  # nopep8
36
+        self._handled_exception_and_http_codes = []  # type: typing.List[typing.Tuple[typing.Type[Exception], int]]  # nopep8
37 37
         self.app = app
38 38
         self.default_error_builder = \
39 39
             default_error_builder or DefaultErrorBuilder()  # FDV
@@ -164,5 +164,7 @@ class FlaskContext(BaseContext):
164 164
         exception_class: typing.Type[Exception],
165 165
         http_code: int,
166 166
     ) -> None:
167
-        self._handled_exceptions.append((exception_class, http_code))
167
+        self._handled_exception_and_http_codes.append(
168
+            (exception_class, http_code),
169
+        )
168 170
 

+ 4 - 2
hapic/ext/pyramid/context.py View File

@@ -32,7 +32,7 @@ class PyramidContext(BaseContext):
32 32
         configurator: 'Configurator',
33 33
         default_error_builder: ErrorBuilderInterface = None,
34 34
     ):
35
-        self._handled_exceptions = []  # type: typing.List[typing.Tuple[typing.Type[Exception], int]]  # nopep8
35
+        self._handled_exception_and_http_codes = []  # type: typing.List[typing.Tuple[typing.Type[Exception], int]]  # nopep8
36 36
         self.configurator = configurator
37 37
         self.default_error_builder = \
38 38
             default_error_builder or DefaultErrorBuilder()  # FDV
@@ -188,4 +188,6 @@ class PyramidContext(BaseContext):
188 188
         exception_class: typing.Type[Exception],
189 189
         http_code: int,
190 190
     ) -> None:
191
-        self._handled_exceptions.append((exception_class, http_code))
191
+        self._handled_exception_and_http_codes.append(
192
+            (exception_class, http_code),
193
+        )

+ 6 - 4
tests/base.py View File

@@ -31,7 +31,7 @@ class MyContext(BottleContext):
31 31
         fake_files_parameters=None,
32 32
     ) -> None:
33 33
         super().__init__(app=app)
34
-        self._handled_exceptions = []  # type: typing.List[typing.Tuple[typing.Type[Exception], int]]  # nopep8
34
+        self._handled_exception_and_http_codes = []  # type: typing.List[typing.Tuple[typing.Type[Exception], int]]  # nopep8
35 35
         self._exceptions_handler_installed = False
36 36
         self.fake_path_parameters = fake_path_parameters or {}
37 37
         self.fake_query_parameters = fake_query_parameters or MultiDict()
@@ -73,9 +73,11 @@ class MyContext(BottleContext):
73 73
     ) -> None:
74 74
         if not self._exceptions_handler_installed:
75 75
             self._install_exceptions_handler()
76
-        self._handled_exceptions.append((exception_class, http_code))
76
+        self._handled_exception_and_http_codes.append(
77
+            (exception_class, http_code),
78
+        )
77 79
 
78
-    def _get_handled_exception_classes(
80
+    def _get_handled_exception_class_and_http_codes(
79 81
         self,
80 82
     ) -> typing.List[typing.Tuple[typing.Type[Exception], int]]:
81
-        return self._handled_exceptions
83
+        return self._handled_exception_and_http_codes