How can I trigger a 500 error in Django?

Old question, but I hope this helps someone in the future. The accepted answer doesn't really answer the original question... You can do this with django rest framework easily by raising ApiException:

from rest_framework.exceptions import APIException

try:
   ...
except ...:
    raise ApiException('custom exception message')

This will return a response with status 500 from your view. Pretty useful if you are calling another function from your API and you don't want to manage return values from this function to decide if returning a response with status 500. You can use it like this:

raise ApiException

And the default response message (at the time of writing this) will be 'A server error occurred.'.

There's a few predefined ApiException subclasses to raise different kinds of errors, check the file rest_framework.exceptions, if you need one that is not in there, you can extend ApiException like this:

from rest_framework.exceptions import APIException


class YourCustomApiExceptionName(APIException):
    def __init__(self, status, detail):
        self.status_code = status
        self.detail = detail

Usage:

raise YourCustomApiExceptionName(100, 'continue')

You can also define custom status and detail but wouldn't make much sense in most cases.


A test view like this will work:

from django.http import HttpResponse

def my_test_500_view(request):
    # Return an "Internal Server Error" 500 response code.
    return HttpResponse(status=500)

or use the baked in error class:

from django.http import HttpResponseServerError
def my_test_500_view(request):
        # Return an "Internal Server Error" 500 response code.
        return HttpResponseServerError()

Raising an appropriate Exception is the most robust way to test this. Returning an HttpResponse of any variety, as in @agconti 's answer, will not allow you to test the behavior of error handling. You'll just see a blank page. Raising an exception triggers both the correct response code and the internal Django behavior you expect with a "real" error.

Response code 500 represents the server not knowing what to do. The easiest way is to write into your test or test-view raise Exception('Make response code 500!').

Most other errors should be raised with exceptions designed to trigger specific response codes. Here's the complete list of Django exceptions that you might raise.

Tags:

Python

Django