Skip to content

Added generic 500 and 400 JSON error handlers. #5904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions docs/api-guide/exceptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,33 @@ The generic views use the `raise_exception=True` flag, which means that you can

By default this exception results in a response with the HTTP status code "400 Bad Request".


---

# Generic Error Views

Django REST Framework provides two error views suitable for providing generic JSON `500` Server Error and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff! We might want to just outline the motivation here, ie “Django’s defaults give you HTML responses”

`400` Bad Request responses. (Django's default error views provide HTML responses, which may not be appropriate for an
API-only application.)

Use these as per [Django's Customizing error views documentation][django-custom-error-views].

## `rest_framework.exceptions.server_error`

Returns a response with status code `500` and `application/json` content type.

Set as `handler500`:

handler500 = 'rest_framework.exceptions.server_error'

## `rest_framework.exceptions.server_error`

Returns a response with status code `400` and `application/json` content type.

Set as `handler400`:

handler400 = 'rest_framework.exceptions.bad_request'

[cite]: https://doughellmann.com/blog/2009/06/19/python-exception-handling-techniques/
[authentication]: authentication.md
[django-custom-error-views]: https://docs.djangoproject.com/en/dev/topics/http/views/#customizing-error-views
21 changes: 21 additions & 0 deletions rest_framework/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import math

from django.http import JsonResponse
from django.utils import six
from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _
Expand Down Expand Up @@ -235,3 +236,23 @@ def __init__(self, wait=None, detail=None, code=None):
wait))))
self.wait = wait
super(Throttled, self).__init__(detail, code)


def server_error(request, *args, **kwargs):
"""
Generic 500 error handler.
"""
data = {
'error': 'Server Error (500)'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would using detail as the key here have been more consistent with other DRF errors? I excitedly ripped out my own custom error handlers for DRF's shiny new ones only to see that the key for error details was now something unexpected.

}
return JsonResponse(data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)


def bad_request(request, exception, *args, **kwargs):
"""
Generic 400 error handler.
"""
data = {
'error': 'Bad Request (400)'
}
return JsonResponse(data, status=status.HTTP_400_BAD_REQUEST)
21 changes: 19 additions & 2 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.test import TestCase
from django.test import RequestFactory, TestCase
from django.utils import six, translation
from django.utils.translation import ugettext_lazy as _

from rest_framework.exceptions import (
APIException, ErrorDetail, Throttled, _get_error_details
APIException, ErrorDetail, Throttled, _get_error_details, bad_request,
server_error
)


Expand Down Expand Up @@ -87,3 +89,18 @@ def test_message(self):
# this test largely acts as a sanity test to ensure the translation files are present.
self.assertEqual(_('A server error occurred.'), 'Une erreur du serveur est survenue.')
self.assertEqual(six.text_type(APIException()), 'Une erreur du serveur est survenue.')


def test_server_error():
request = RequestFactory().get('/')
response = server_error(request)
assert response.status_code == 500
assert response["content-type"] == 'application/json'


def test_bad_request():
request = RequestFactory().get('/')
exception = Exception('Something went wrong — Not used')
response = bad_request(request, exception)
assert response.status_code == 400
assert response["content-type"] == 'application/json'