diff options
-rw-r--r-- | lcm/jobs/views.py | 38 |
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) |