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
         self,
153
         self,
154
         func: typing.Callable[..., typing.Any],
154
         func: typing.Callable[..., typing.Any],
155
     ) -> typing.Callable[..., typing.Any]:
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
         def decorator(*args, **kwargs):
163
         def decorator(*args, **kwargs):
157
             try:
164
             try:
158
                 return func(*args, **kwargs)
165
                 return func(*args, **kwargs)
159
             except Exception as exc:
166
             except Exception as exc:
160
                 # Reverse list to read first user given exception before
167
                 # Reverse list to read first user given exception before
161
                 # the hapic default Exception catch
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
                 for handled_exception_class, http_code in handled:
172
                 for handled_exception_class, http_code in handled:
164
                     # TODO BS 2018-05-04: How to be attentive to hierarchy ?
173
                     # TODO BS 2018-05-04: How to be attentive to hierarchy ?
165
                     if isinstance(exc, handled_exception_class):
174
                     if isinstance(exc, handled_exception_class):
173
 
182
 
174
         return decorator
183
         return decorator
175
 
184
 
176
-    def _get_handled_exception_classes(
185
+    def _get_handled_exception_class_and_http_codes(
177
         self,
186
         self,
178
     ) -> typing.List[typing.Tuple[typing.Type[Exception], int]]:
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
         raise NotImplementedError()
193
         raise NotImplementedError()
180
 
194
 
181
     def _add_exception_class_to_catch(
195
     def _add_exception_class_to_catch(
183
         exception_class: typing.Type[Exception],
197
         exception_class: typing.Type[Exception],
184
         http_code: int,
198
         http_code: int,
185
     ) -> None:
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
         raise NotImplementedError()
207
         raise NotImplementedError()

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

33
         app: bottle.Bottle,
33
         app: bottle.Bottle,
34
         default_error_builder: ErrorBuilderInterface=None,
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
         self.app = app
37
         self.app = app
38
         self.default_error_builder = \
38
         self.default_error_builder = \
39
             default_error_builder or DefaultErrorBuilder()  # FDV
39
             default_error_builder or DefaultErrorBuilder()  # FDV
141
         exception_class: typing.Type[Exception],
141
         exception_class: typing.Type[Exception],
142
         http_code: int,
142
         http_code: int,
143
     ) -> None:
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
     def _install_exceptions_handler(self) -> None:
148
     def _install_exceptions_handler(self) -> None:
147
         self.app.install(self.handle_exceptions_decorator_builder)
149
         self.app.install(self.handle_exceptions_decorator_builder)

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

33
         app: Flask,
33
         app: Flask,
34
         default_error_builder: ErrorBuilderInterface=None,
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
         self.app = app
37
         self.app = app
38
         self.default_error_builder = \
38
         self.default_error_builder = \
39
             default_error_builder or DefaultErrorBuilder()  # FDV
39
             default_error_builder or DefaultErrorBuilder()  # FDV
164
         exception_class: typing.Type[Exception],
164
         exception_class: typing.Type[Exception],
165
         http_code: int,
165
         http_code: int,
166
     ) -> None:
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
         configurator: 'Configurator',
32
         configurator: 'Configurator',
33
         default_error_builder: ErrorBuilderInterface = None,
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
         self.configurator = configurator
36
         self.configurator = configurator
37
         self.default_error_builder = \
37
         self.default_error_builder = \
38
             default_error_builder or DefaultErrorBuilder()  # FDV
38
             default_error_builder or DefaultErrorBuilder()  # FDV
188
         exception_class: typing.Type[Exception],
188
         exception_class: typing.Type[Exception],
189
         http_code: int,
189
         http_code: int,
190
     ) -> None:
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
         fake_files_parameters=None,
31
         fake_files_parameters=None,
32
     ) -> None:
32
     ) -> None:
33
         super().__init__(app=app)
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
         self._exceptions_handler_installed = False
35
         self._exceptions_handler_installed = False
36
         self.fake_path_parameters = fake_path_parameters or {}
36
         self.fake_path_parameters = fake_path_parameters or {}
37
         self.fake_query_parameters = fake_query_parameters or MultiDict()
37
         self.fake_query_parameters = fake_query_parameters or MultiDict()
73
     ) -> None:
73
     ) -> None:
74
         if not self._exceptions_handler_installed:
74
         if not self._exceptions_handler_installed:
75
             self._install_exceptions_handler()
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
         self,
81
         self,
80
     ) -> typing.List[typing.Tuple[typing.Type[Exception], int]]:
82
     ) -> typing.List[typing.Tuple[typing.Type[Exception], int]]:
81
-        return self._handled_exceptions
83
+        return self._handled_exception_and_http_codes