Returns the response that should be used for any given exception. By default we handle the REST framework `APIException`, and also Django's built-in `Http404` and `PermissionDenied` exceptions. Any unhandled exceptions may return `None`, which will cause a 500 error to be rais
(exc, context)
| 70 | |
| 71 | |
| 72 | def exception_handler(exc, context): |
| 73 | """ |
| 74 | Returns the response that should be used for any given exception. |
| 75 | |
| 76 | By default we handle the REST framework `APIException`, and also |
| 77 | Django's built-in `Http404` and `PermissionDenied` exceptions. |
| 78 | |
| 79 | Any unhandled exceptions may return `None`, which will cause a 500 error |
| 80 | to be raised. |
| 81 | """ |
| 82 | if isinstance(exc, Http404): |
| 83 | exc = exceptions.NotFound(*(exc.args)) |
| 84 | elif isinstance(exc, PermissionDenied): |
| 85 | exc = exceptions.PermissionDenied(*(exc.args)) |
| 86 | |
| 87 | if isinstance(exc, exceptions.APIException): |
| 88 | headers = {} |
| 89 | if getattr(exc, 'auth_header', None): |
| 90 | headers['WWW-Authenticate'] = exc.auth_header |
| 91 | if getattr(exc, 'wait', None): |
| 92 | headers['Retry-After'] = '%d' % exc.wait |
| 93 | |
| 94 | if isinstance(exc.detail, (list, dict)): |
| 95 | data = exc.detail |
| 96 | else: |
| 97 | data = {'detail': exc.detail} |
| 98 | |
| 99 | set_rollback() |
| 100 | return Response(data, status=exc.status_code, headers=headers) |
| 101 | |
| 102 | return None |
| 103 | |
| 104 | |
| 105 | class APIView(View): |
no test coverage detected