diff options
author | hongyuzhao <zhao.hongyu@zte.com.cn> | 2019-09-29 17:14:19 +0800 |
---|---|---|
committer | hongyuzhao <zhao.hongyu@zte.com.cn> | 2019-09-29 17:14:28 +0800 |
commit | 71c2d7ce3e12bcb305d3a746bc77323c814ce0ef (patch) | |
tree | da805bce06f5097f5c825840fdb648d389337fd4 | |
parent | d6d07ea345ddf22a1e4936cd83b62b1970d27ce5 (diff) |
Code optimization5
Change-Id: Ibaace481027bf0a6dcdc31386224626236c534a0
Issue-ID: VFC-1431
Signed-off-by: hongyuzhao <zhao.hongyu@zte.com.cn>
-rw-r--r-- | lcm/ns_sfcs/views/views.py | 46 |
1 files changed, 43 insertions, 3 deletions
diff --git a/lcm/ns_sfcs/views/views.py b/lcm/ns_sfcs/views/views.py index 57e7346a..68abd9dc 100644 --- a/lcm/ns_sfcs/views/views.py +++ b/lcm/ns_sfcs/views/views.py @@ -32,12 +32,50 @@ from lcm.ns_sfcs.biz.sfc_instance import SfcInstance from rest_framework import status from rest_framework.response import Response from rest_framework.views import APIView +from lcm.pub.exceptions import BadRequestException, NSLCMException from lcm.ns_sfcs.biz.utils import get_fp_id, ignorcase_get logger = logging.getLogger(__name__) +def view_safe_call_with_log(logger): + def view_safe_call(func): + def wrapper(*args, **kwargs): + try: + return func(*args, **kwargs) + except BadRequestException as e: + logger.error(e.args[0]) + return make_error_resp( + detail=e.args[0], + status=status.HTTP_400_BAD_REQUEST + ) + except NSLCMException as e: + logger.error(e.args[0]) + return make_error_resp( + detail=e.args[0], + status=status.HTTP_500_INTERNAL_SERVER_ERROR + ) + except Exception as e: + logger.error(e.args[0]) + logger.error(traceback.format_exc()) + return make_error_resp( + detail='Unexpected exception', + status=status.HTTP_500_INTERNAL_SERVER_ERROR + ) + return wrapper + return view_safe_call + + +def make_error_resp(status, detail): + return Response( + data={ + 'error': detail + }, + status=status + ) + + class SfcInstanceView(APIView): @swagger_auto_schema( request_body=CreateSfcInstReqSerializer(), @@ -46,11 +84,12 @@ class SfcInstanceView(APIView): status.HTTP_500_INTERNAL_SERVER_ERROR: "Inner error" } ) + @view_safe_call_with_log(logger) def post(self, request): try: req_serializer = CreateSfcInstReqSerializer(data=request.data) if not req_serializer.is_valid(): - raise Exception(req_serializer.errors) + raise BadRequestException(req_serializer.errors) data = { 'nsinstid': request.data['nsInstanceId'], @@ -62,7 +101,7 @@ class SfcInstanceView(APIView): resp_serializer = CreateSfcInstRespSerializer(data=rsp) if not resp_serializer.is_valid(): - raise Exception(resp_serializer.errors) + raise NSLCMException(resp_serializer.errors) return Response(data=rsp, status=status.HTTP_200_OK) except Exception as e: @@ -135,6 +174,7 @@ class SfcView(APIView): status.HTTP_200_OK: CreateSfcRespSerializer() } ) + @view_safe_call_with_log(logger) def post(self, request): try: logger.info("Create Service Function Chain start") @@ -148,7 +188,7 @@ class SfcView(APIView): req_serializer = CreateSfcReqSerializer(data=request.data) if not req_serializer.is_valid(): - raise Exception(req_serializer.errors) + raise BadRequestException(req_serializer.errors) except Exception as e: logger.error("Exception occurs: %s", e.args[0]) logger.error(traceback.format_exc()) |