How can I trigger a 500 error in Django?

Question:

I’m trying to setup email error logging and I want to test it.

Whats an easy way to trigger a 500 error in Django? This surprisingly has not been discussed here yet.

Asked By: User

||

Answers:

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()
Answered By: agconti

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.

Answered By: Shaun Overton

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.

Answered By: nnov

If you can or want to do without custom view-code, here is a variant that uses only urls.py:

# django urls.py

from django.urls import path
from django.views.defaults import server_error

urlpatterns = [
    path('500/', server_error),
]
Answered By: tombreit
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.