summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhongyuzhao <zhao.hongyu@zte.com.cn>2019-09-29 16:05:06 +0800
committerhongyuzhao <zhao.hongyu@zte.com.cn>2019-09-29 16:49:33 +0800
commit9d2b1d6e211465ccdef88bf41f49fea0cf28980d (patch)
treeb79cced51982932e43ad450f6e5fa57965c6e326
parent1edc360ac43f5f6d82ae64ec1e5dd2efe39fcd81 (diff)
Code optimization3
Change-Id: Ie41a2b8ffa37c9e921cce7b76a3cd43ad28cdb05 Issue-ID: VFC-1431 Signed-off-by: hongyuzhao <zhao.hongyu@zte.com.cn>
-rw-r--r--lcm/jobs/views.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/lcm/jobs/views.py b/lcm/jobs/views.py
index e2c1e9b8..ff19ad05 100644
--- a/lcm/jobs/views.py
+++ b/lcm/jobs/views.py
@@ -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)