python - Error handling in Django REST framework - empty file returned instead of Error view -
i have piece of code in django rest framework looks following. works if request arguments valid, if specify non-valid search keyword, browser returns empty python file length of 0 bytes instead of html error code. doing wrong respect error catching?
from rest_framework.response import response rest_framework.views import apiview rest_framework.renderers import jsonrenderer, statichtmlrenderer rest_framework.exceptions import parseerror class getdataseriesid(apiview): renderer_classes = (jsonrenderer, statichtmlrenderer) def get(self, request, format=none): # check format keyword , overrule default rendering format if 'format' in request.query_params: request.accepted_renderer.format = request.query_params['format'] # build keyword arguments (left out clarity) search_dict = {'station_name':'example'} # render query try: res = dataseriesidserializer(**search_dict) except exception e: raise parseerror("malformed rest service request.\ndetail: %s", e) return response(res.data)
the server error log reports psycopsg2 error wanted generate:
[tue apr 19 13:09:53 2016] [error] error when executing query select [...] [tue apr 19 13:09:53 2016] [error] column s.station_itgd not exist indeed error wanted generate.
the issue thought code capture error , generate proper eror view. doesn't happen. aware catching exception not practice - first step. once response work analyze errors may occur , improve part.
in drf view, supposed return response object. could, instead of raising parseerror, rest_framework import status return response({'detail':"your error message"}, status=status.http_400_bad_request),
uncatched exception return response(status=status.http_500_internal_server_error)
Comments
Post a Comment