A
A
albertalexandrov2019-12-20 17:59:23
Django
albertalexandrov, 2019-12-20 17:59:23

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

1 answer(s)
A
antonksa, 2019-12-20
@albertalexandrov

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'
}

PS. aimsorry, misunderstood the question. In fact, if your DRF processes the URL, then by default DRF processes 500, if it doesn’t work for you, then you are doing something wrong.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question