Answer the question
In order to leave comments, you need to log in
How to override handler500?
Hello everyone!)
Please tell me how and where to override the default handler500 on drf's rest_framework.exceptions.APIException?
Answer the question
In order to leave comments, you need to log in
https://www.django-rest-framework.org/api-guide/ex...
# my_project/my_app/utils.py
from rest_framework.exceptions import APIException
from rest_framework.views import exception_handler
def custom_exception_handler(exc, context):
# Call REST framework's default exception handler first,
# to get the standard error response.
response = exception_handler(exc, context)
# Now add the HTTP status code to the response.
if response is not None:
if response.status_code == 500:
# My custom 500 error handler.
pass
return response
# settings.py
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'my_project.my_app.utils.custom_exception_handler'
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question