diff options
author | maopeng zhang <zhang.maopeng1@zte.com.cn> | 2019-09-29 09:15:09 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-09-29 09:15:09 +0000 |
commit | 7c36563f49cda5c9258bce3db0c731613b7e8251 (patch) | |
tree | 85cf61055c0935e0809b4d32571aa5f8549f94cf | |
parent | 3c8c20ec43b670acc0e185c448b87c5f5b4e3db0 (diff) | |
parent | d6d07ea345ddf22a1e4936cd83b62b1970d27ce5 (diff) |
Merge "Code optimization4"
-rw-r--r-- | lcm/ns_vnfs/views/views.py | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/lcm/ns_vnfs/views/views.py b/lcm/ns_vnfs/views/views.py index b2c07ecd..44c5f015 100644 --- a/lcm/ns_vnfs/views/views.py +++ b/lcm/ns_vnfs/views/views.py @@ -49,10 +49,48 @@ from lcm.ns_vnfs.biz.verify_vnfs import VerifyVnfs from lcm.ns_vnfs.serializers.serializers import VimInfoRespSerializer from lcm.ns_vnfs.serializers.serializers import VnfmInfoRespSerializer from lcm.ns_vnfs.biz import create_vnfs +from lcm.pub.exceptions import BadRequestException 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 NfView(APIView): @swagger_auto_schema( request_body=InstVnfReqSerializer(), @@ -241,12 +279,13 @@ class NfScaleView(APIView): status.HTTP_409_CONFLICT: "Inner error" } ) + @view_safe_call_with_log(logger) def post(self, request, vnfinstid): logger.debug("NfScaleView--post::> %s" % request.data) try: req_serializer = ScaleVnfReqSerializer(data=request.data) if not req_serializer.is_valid(): - raise Exception(req_serializer.errors) + raise BadRequestException(req_serializer.errors) NFManualScaleService(vnfinstid, request.data).start() return Response(data={}, status=status.HTTP_202_ACCEPTED) except Exception as e: @@ -262,13 +301,14 @@ class NfVerifyView(APIView): status.HTTP_409_CONFLICT: "Inner error" } ) + @view_safe_call_with_log(logger) def post(self, request): job_id = "VNFSDK_" + str(uuid.uuid4()) logger.debug("NfVerifyView--post::%s> %s", job_id, request.data) try: req_serializer = VerifyVnfReqSerializer(data=request.data) if not req_serializer.is_valid(): - raise Exception(req_serializer.errors) + raise BadRequestException(req_serializer.errors) VerifyVnfs(request.data, job_id).start() |