|
@@ -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()
|