Code optimization3 84/96384/4
authorhongyuzhao <zhao.hongyu@zte.com.cn>
Sun, 29 Sep 2019 08:05:06 +0000 (16:05 +0800)
committerhongyuzhao <zhao.hongyu@zte.com.cn>
Sun, 29 Sep 2019 08:49:33 +0000 (16:49 +0800)
Change-Id: Ie41a2b8ffa37c9e921cce7b76a3cd43ad28cdb05
Issue-ID: VFC-1431
Signed-off-by: hongyuzhao <zhao.hongyu@zte.com.cn>
lcm/jobs/views.py

index e2c1e9b..ff19ad0 100644 (file)
@@ -31,6 +31,43 @@ from lcm.pub.exceptions import BadRequestException, NSLCMException
 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 JobView(APIView):
 
     input_job_id = openapi.Parameter(
@@ -74,6 +111,7 @@ class JobView(APIView):
             status.HTTP_202_ACCEPTED: JobUpdRespSerializer()
         }
     )
+    @view_safe_call_with_log(logger=logger)
     def post(self, request, job_id):
         try:
             logger.debug("Enter JobView:post, job_id=%s, request=%s", job_id, request.data)