defexception_handler(exc, context): """ 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 raised. """ if isinstance(exc, Http404): exc = exceptions.NotFound() elif isinstance(exc, PermissionDenied): exc = exceptions.PermissionDenied()
if isinstance(exc, exceptions.APIException): headers = {} if getattr(exc, 'auth_header', None): headers['WWW-Authenticate'] = exc.auth_header if getattr(exc, 'wait', None): headers['Retry-After'] = '%d' % exc.wait
if isinstance(exc.detail, (list, dict)): data = exc.detail else: data = {'detail': exc.detail}
# Exceptions form DRF and Django built-in `Http404` and `PermissionDenied` if response isnotNone: if isinstance(response.data, list): msg = '; '.join(response.data) elif isinstance(response.data, str): msg = response.data else: msg = 'Sorry, we make a mistake (* ̄︶ ̄)!'