|
@@ -11,18 +11,6 @@ from hapic.processor import RequestParameters
|
11
|
11
|
|
12
|
12
|
|
13
|
13
|
class ControllerWrapper(object):
|
14
|
|
- def __init__(
|
15
|
|
- self,
|
16
|
|
- context: ContextInterface,
|
17
|
|
- processor: ProcessorInterface,
|
18
|
|
- error_http_code: HTTPStatus=HTTPStatus.BAD_REQUEST,
|
19
|
|
- default_http_code: HTTPStatus=HTTPStatus.OK,
|
20
|
|
- ) -> None:
|
21
|
|
- self.context = context
|
22
|
|
- self.processor = processor
|
23
|
|
- self.error_http_code = error_http_code
|
24
|
|
- self.default_http_code = default_http_code
|
25
|
|
-
|
26
|
14
|
def before_wrapped_func(
|
27
|
15
|
self,
|
28
|
16
|
func_args: typing.Tuple[typing.Any, ...],
|
|
@@ -44,13 +32,35 @@ class ControllerWrapper(object):
|
44
|
32
|
if replacement_response:
|
45
|
33
|
return replacement_response
|
46
|
34
|
|
47
|
|
- response = func(*args, **kwargs)
|
|
35
|
+ response = self._execute_wrapped_function(func, args, kwargs)
|
48
|
36
|
new_response = self.after_wrapped_function(response)
|
49
|
37
|
return new_response
|
50
|
38
|
return wrapper
|
51
|
39
|
|
|
40
|
+ def _execute_wrapped_function(
|
|
41
|
+ self,
|
|
42
|
+ func,
|
|
43
|
+ func_args,
|
|
44
|
+ func_kwargs,
|
|
45
|
+ ) -> typing.Any:
|
|
46
|
+ return func(*func_args, **func_kwargs)
|
|
47
|
+
|
52
|
48
|
|
53
|
|
-class InputControllerWrapper(ControllerWrapper):
|
|
49
|
+class InputOutputControllerWrapper(ControllerWrapper):
|
|
50
|
+ def __init__(
|
|
51
|
+ self,
|
|
52
|
+ context: ContextInterface,
|
|
53
|
+ processor: ProcessorInterface,
|
|
54
|
+ error_http_code: HTTPStatus=HTTPStatus.BAD_REQUEST,
|
|
55
|
+ default_http_code: HTTPStatus=HTTPStatus.OK,
|
|
56
|
+ ) -> None:
|
|
57
|
+ self.context = context
|
|
58
|
+ self.processor = processor
|
|
59
|
+ self.error_http_code = error_http_code
|
|
60
|
+ self.default_http_code = default_http_code
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+class InputControllerWrapper(InputOutputControllerWrapper):
|
54
|
64
|
def before_wrapped_func(
|
55
|
65
|
self,
|
56
|
66
|
func_args: typing.Tuple[typing.Any, ...],
|
|
@@ -122,7 +132,7 @@ class InputControllerWrapper(ControllerWrapper):
|
122
|
132
|
return error_response
|
123
|
133
|
|
124
|
134
|
|
125
|
|
-class OutputControllerWrapper(ControllerWrapper):
|
|
135
|
+class OutputControllerWrapper(InputOutputControllerWrapper):
|
126
|
136
|
def __init__(
|
127
|
137
|
self,
|
128
|
138
|
context: ContextInterface,
|
|
@@ -130,10 +140,12 @@ class OutputControllerWrapper(ControllerWrapper):
|
130
|
140
|
error_http_code: HTTPStatus=HTTPStatus.INTERNAL_SERVER_ERROR,
|
131
|
141
|
default_http_code: HTTPStatus=HTTPStatus.OK,
|
132
|
142
|
) -> None:
|
133
|
|
- self.context = context
|
134
|
|
- self.processor = processor
|
135
|
|
- self.error_http_code = error_http_code
|
136
|
|
- self.default_http_code = default_http_code
|
|
143
|
+ super().__init__(
|
|
144
|
+ context,
|
|
145
|
+ processor,
|
|
146
|
+ error_http_code,
|
|
147
|
+ default_http_code,
|
|
148
|
+ )
|
137
|
149
|
|
138
|
150
|
def get_error_response(
|
139
|
151
|
self,
|
|
@@ -271,3 +283,42 @@ class InputFormsControllerWrapper(InputControllerWrapper):
|
271
|
283
|
request_parameters.form_parameters,
|
272
|
284
|
)
|
273
|
285
|
return processed_data
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+class ExceptionHandlerControllerWrapper(ControllerWrapper):
|
|
289
|
+ def __init__(
|
|
290
|
+ self,
|
|
291
|
+ handled_exception_class: typing.Type[Exception],
|
|
292
|
+ context: ContextInterface,
|
|
293
|
+ http_code: HTTPStatus=HTTPStatus.INTERNAL_SERVER_ERROR,
|
|
294
|
+ ) -> None:
|
|
295
|
+ self.handled_exception_class = handled_exception_class
|
|
296
|
+ self.context = context
|
|
297
|
+ self.http_code = http_code
|
|
298
|
+
|
|
299
|
+ def _execute_wrapped_function(
|
|
300
|
+ self,
|
|
301
|
+ func,
|
|
302
|
+ func_args,
|
|
303
|
+ func_kwargs,
|
|
304
|
+ ) -> typing.Any:
|
|
305
|
+ try:
|
|
306
|
+ return super()._execute_wrapped_function(
|
|
307
|
+ func,
|
|
308
|
+ func_args,
|
|
309
|
+ func_kwargs,
|
|
310
|
+ )
|
|
311
|
+ except self.handled_exception_class as exc:
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+ error_dict = {
|
|
315
|
+ 'error_message': str(exc),
|
|
316
|
+ }
|
|
317
|
+ if hasattr(exc, 'error_dict'):
|
|
318
|
+ error_dict.update(exc.error_dict)
|
|
319
|
+
|
|
320
|
+ error_response = self.context.get_response(
|
|
321
|
+ error_dict,
|
|
322
|
+ self.http_code,
|
|
323
|
+ )
|
|
324
|
+ return error_response
|