'Error handlers with Django Rest Framework are never catch

In my DRF app, I would catch the "Not Found: ..." urls (/v1/blabla/ for example), and using the ErrorHandler. So, the error is :

Not Found: /v1/gdfgfd/
[09/Mar/2022 11:05:12] "GET /v1/gdfgfd/ HTTP/1.1" 404 4663

In the my_app/urls.py, I do this :

def page_not_found_error_handler(request, exception=None):
    print(">>> error page_not_found_error_handler !!")
    return Response({"message": "Oups, I did it again"})    

def bad_request_error_handler(request, exception=None):
    print(">>> error bad_request_error_handler !!")
    return Response({"message": "Oups, I did it again"})


# Errors handler
handler404 = page_not_found_error_handler
handler400 = bad_request_error_handler

But it's not working.

I'll do that too :

from .views import errors    
handler404 = 'my_app.views.errors.page_not_found_error_handler'

This line make an error :

ERRORS:
?: (urls.E008) The custom handler404 view 'api_massifs.views.errors.page_not_found_error_handler' could not be imported.
    HINT: Could not import 'api_massifs.views.errors.page_not_found_error_handler'. View does not exist in module api_massifs.views.errors.

The file is located here. My views (and the serializers) are dispatched in a "my_app/views/*.py" folder :

my_project/my_app/views/errors.py
...
my_project/my_app/fixtures
my_project/my_app/migrations
my_project/my_app/serializers
my_project/my_app/...

Have you an idea please ?

Thanks F.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source