I currently have some code for a view based on the Django REST Framework. Ive been using a customer exception class, but ideally I want to use the inbuilt Django REST exceptions.
From the code below I feel this probably not the best or cleanest way to utilize the REST Framework exceptions to its maximum.
Has anyone got any good examples where they are catching issues and returning them cleanly with the REST built in exceptions ?
class JSONResponse(HttpResponse):
def __init__(self, data, **kwargs):
content = JSONRenderer().render(data)
kwargs['content_type'] = 'application/json'
super(JSONResponse, self).__init__(content, **kwargs)
def queryInput(request):
try:
auth_token = session_id = getAuthHeader(request)
if not auth_token:
return JSONResponse({'detail' : "fail", "error" : "No X-Auth-Token Found", "data" : None}, status=500)
if request.method:
data = JSONParser().parse(request)
serializer = queryInputSerializer(data=data)
if request.method == 'POST':
if serializer.is_valid():
input= serializer.data["input"]
fetchData = MainRunner(input=input,auth_token=auth_token)
main_data = fetchData.main()
if main_data:
return JSONResponse({'detail' : "success", "error" : None, "data" : main_data}, status=201)
return JSONResponse({'detail' : "Unknown Error","error" : True, "data" : None}, status=500)
except Exception as e:
return JSONResponse({'error' : str(e)},status=500)
You can use the build in DRF exception, just import and raise
The Django REST framework provides several built in exceptions, which are mostly subclasses of DRF's
APIException
.You can raise exceptions in your view like you normally would in Python:
You could also create your own custom exception by inheriting from
APIException
and settingstatus_code
anddefault_detail
. Some of the built in ones are:ParseError
,AuthenticationFailed
,NotAuthenticated
,PermissionDenied
,NotFound
,NotAcceptable
,ValidationError
, etc.These will then get converted to a
Response
by the REST Framework's exception handler. Each exception is associated with a status code that is added to theResponse
. By default the exception handler is set to the built in handler:But you can set it to your own custom exception handler if you want to convert the exceptions yourself by changing this in your
settings.py
file:And then create the custom handler in that location: