Created
January 15, 2014 00:41
-
-
Save jpadilla/8428798 to your computer and use it in GitHub Desktop.
Custom exception handler for Django Rest Framework that adds the `status_code` to the response and renames the `detail` key to `error`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework.views import exception_handler | |
def custom_exception_handler(exc): | |
""" | |
Custom exception handler for Django Rest Framework that adds | |
the `status_code` to the response and renames the `detail` key to `error`. | |
""" | |
response = exception_handler(exc) | |
if response is not None: | |
response.data['status_code'] = response.status_code | |
response.data['error'] = response.data['detail'] | |
del response.data['detail'] | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Worth noting that the custom exception handler has to be activated in the settings.