In a new project custom exception_handler is working,but to my old project it's not working, i follow the doc custom exception handler
to writing. And I set print in my exception_handler, but it's not show any thing.(exception will be catch but using the default handler.)
# myapp.exception.py
from rest_framework.views import exception_handler, Response
from rest_framework.exceptions import APIException
def custom_exception(exc, context):
response = exception_handler(exc, context)
print(exc)
if response is not None:
response.data['exception'] = 'h'
else:
response = Response(data={'abc':123}, status=500)
return response
# settings.py
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'basuser.exception.custom_exception'
}
INSTALLED_APPS = [
'...',
'...',
'rest_framework'
]
#views.py
class CustomException(APIException):
status_code = 401
default_detail = 'detail exception'
default_code = 'custom_exception'
class TestPage(APIView):
def get(self, request,format=None):
return Response({'data':100})
def post(self, request):
raise CustomException()
# out put
Unauthorized: /testpage/
[03/Feb/2022 03:33:21] "POST /testpage/ HTTP/1.1" 401 21
# json response
{
"detail": "detail exception"
}
It's so confused...
Solved, It's setting.py.REST_FRAMEWORK['EXCEPTION_HANDLER'] is no effect.
rest_settings.DEFAULTS['EXCEPTION_HANDLER'] = 'basuser.exception.custom_exception'