Coverage for api/utils/exceptions.py: 100%

7 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 06:14 +0000

1from rest_framework.exceptions import ValidationError 

2from rest_framework.views import exception_handler as drf_exception_handler 

3 

4 

5def exception_handler(ex, context): 

6 """ 

7 Handle the exception raised in a DRF context. 

8 

9 See `DRF docs`_. 

10 .. _DRF docs: https://www.django-rest-framework.org/api-guide/exceptions/#custom-exception-handling 

11 

12 :param ex: the exception that has occurred 

13 :param context: additional data about the context of the exception 

14 :return: the response to show for the exception 

15 """ # noqa: E501 

16 

17 res = drf_exception_handler(ex, context) 

18 if isinstance(ex, ValidationError): 

19 # Wrap validation errors inside a `detail` key for consistency 

20 res.data = {"detail": res.data} 

21 return res