|
@@ -564,24 +564,65 @@ class ExceptionHandlerControllerWrapper(ControllerWrapper):
|
564
|
564
|
func_kwargs,
|
565
|
565
|
)
|
566
|
566
|
except self.handled_exception_class as exc:
|
567
|
|
- response_content = self.error_builder.build_from_exception(
|
568
|
|
- exc,
|
569
|
|
- include_traceback=self.context.is_debug(),
|
570
|
|
- )
|
|
567
|
+ return self._build_error_response(exc)
|
|
568
|
+
|
|
569
|
+ def _build_error_response(self, exc: Exception) -> typing.Any:
|
|
570
|
+ response_content = self.error_builder.build_from_exception(
|
|
571
|
+ exc,
|
|
572
|
+ include_traceback=self.context.is_debug(),
|
|
573
|
+ )
|
571
|
574
|
|
572
|
|
- # Check error format
|
573
|
|
- dumped = self.error_builder.dump(response_content).data
|
574
|
|
- unmarshall = self.error_builder.load(dumped)
|
575
|
|
- if unmarshall.errors:
|
576
|
|
- raise OutputValidationException(
|
577
|
|
- 'Validation error during dump of error response: {}'
|
|
575
|
+ # Check error format
|
|
576
|
+ dumped = self.error_builder.dump(response_content).data
|
|
577
|
+ unmarshall = self.error_builder.load(dumped)
|
|
578
|
+ if unmarshall.errors:
|
|
579
|
+ raise OutputValidationException(
|
|
580
|
+ 'Validation error during dump of error response: {}'
|
578
|
581
|
.format(
|
579
|
|
- str(unmarshall.errors)
|
580
|
|
- )
|
|
582
|
+ str(unmarshall.errors)
|
581
|
583
|
)
|
|
584
|
+ )
|
|
585
|
+
|
|
586
|
+ error_response = self.context.get_response(
|
|
587
|
+ json.dumps(dumped),
|
|
588
|
+ self.http_code,
|
|
589
|
+ )
|
|
590
|
+ return error_response
|
|
591
|
+
|
582
|
592
|
|
583
|
|
- error_response = self.context.get_response(
|
584
|
|
- json.dumps(dumped),
|
585
|
|
- self.http_code,
|
|
593
|
+# TODO BS 2018-07-23: This class is an async version of
|
|
594
|
+# ExceptionHandlerControllerWrapper
|
|
595
|
+# to permit async compatibility. Please re-think about code refact
|
|
596
|
+# TAG: REFACT_ASYNC
|
|
597
|
+class AsyncExceptionHandlerControllerWrapper(ExceptionHandlerControllerWrapper):
|
|
598
|
+ def get_wrapper(
|
|
599
|
+ self,
|
|
600
|
+ func: 'typing.Callable[..., typing.Any]',
|
|
601
|
+ ) -> 'typing.Callable[..., typing.Any]':
|
|
602
|
+ # async def wrapper(*args, **kwargs) -> typing.Any:
|
|
603
|
+ async def wrapper(*args, **kwargs) -> typing.Any:
|
|
604
|
+ # Note: Design of before_wrapped_func can be to update kwargs
|
|
605
|
+ # by reference here
|
|
606
|
+ replacement_response = self.before_wrapped_func(args, kwargs)
|
|
607
|
+ if replacement_response is not None:
|
|
608
|
+ return replacement_response
|
|
609
|
+
|
|
610
|
+ response = await self._execute_wrapped_function(func, args, kwargs)
|
|
611
|
+ new_response = self.after_wrapped_function(response)
|
|
612
|
+ return new_response
|
|
613
|
+ return functools.update_wrapper(wrapper, func)
|
|
614
|
+
|
|
615
|
+ async def _execute_wrapped_function(
|
|
616
|
+ self,
|
|
617
|
+ func,
|
|
618
|
+ func_args,
|
|
619
|
+ func_kwargs,
|
|
620
|
+ ) -> typing.Any:
|
|
621
|
+ try:
|
|
622
|
+ return await super()._execute_wrapped_function(
|
|
623
|
+ func,
|
|
624
|
+ func_args,
|
|
625
|
+ func_kwargs,
|
586
|
626
|
)
|
587
|
|
- return error_response
|
|
627
|
+ except self.handled_exception_class as exc:
|
|
628
|
+ return self._build_error_response(exc)
|