Handling invalid UUIDs in URL parameters · Issue #3377 · encode/django-rest-framework (original) (raw)

I have been looking around forums and within the DRF issues but cant seem to find any examples how to handle invalid UUIDs passed in via browser.

My REST end point is as follows using default SimpleRouter

http://localhost/api/v1/customers/3d70aa0089b64e32a358af90c5df57272b7

router = routers.SimpleRouter(trailing_slash=True)
router.register(r'customers', views.CustomerViewSet, 'customer')

If I was to pass in an invalid UUID i would get html response saying

ValueError: badly formed hexadecimal UUID string

Is they any way catch ValueError and return a custom JSON response such as invalid id that can be applied globally as most of my primary keys are UUIDs. Appreciate if anyone can point me to the right direction.