32 lines
689 B
Python
32 lines
689 B
Python
import functools
|
|
import traceback
|
|
|
|
from django.conf import settings
|
|
from django.http import JsonResponse
|
|
|
|
|
|
def ret(json_object, status=200):
|
|
return JsonResponse(
|
|
json_object, status=status, safe=not isinstance(json_object, list)
|
|
)
|
|
|
|
|
|
def error_response(exception):
|
|
res = {
|
|
'errorMessage': str(exception),
|
|
'traceback': traceback.format_exc() if settings.DEBUG else 'error occured',
|
|
}
|
|
|
|
return ret(res, status=400)
|
|
|
|
|
|
def base_view(fn):
|
|
@functools.wraps(fn)
|
|
def inner(request, *args, **kwargs):
|
|
try:
|
|
return fn(request, *args, **kwargs)
|
|
except Exception as e:
|
|
return error_response(e)
|
|
|
|
return inner
|