1 # Copyright 2019 ZTE Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
18 from rest_framework import status
19 from rest_framework.response import Response
21 from lcm.pub.exceptions import BadRequestException
22 from lcm.pub.exceptions import NSLCMException
24 logger = logging.getLogger(__name__)
27 def make_error_resp(status, detail):
37 def view_safe_call_with_log(logger):
38 def view_safe_call(func):
39 def wrapper(*args, **kwargs):
41 return func(*args, **kwargs)
42 except BadRequestException as e:
43 logger.error(e.args[0])
44 return make_error_resp(
46 status=status.HTTP_400_BAD_REQUEST
48 except NSLCMException as e:
49 logger.error(e.args[0])
50 return make_error_resp(
52 status=status.HTTP_500_INTERNAL_SERVER_ERROR
54 except Exception as e:
55 logger.error(e.args[0])
56 logger.error(traceback.format_exc())
57 return make_error_resp(
58 detail='Unexpected exception',
59 status=status.HTTP_500_INTERNAL_SERVER_ERROR