User
User

Reputation: 24741

How can I trigger a 500 error in Django?

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.

Upvotes: 52

Views: 53954

Answers (4)

Shaun Overton
Shaun Overton

Reputation: 621

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.

Upvotes: 22

nnov
nnov

Reputation: 541

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.

EDIT:

If for some reason you are working without DRF, you can just copy the APIException code instead of installing the DRF package:

https://github.com/encode/django-rest-framework/blob/4f7e9ed3bbb0fb3567884ef4e3d340bc2f77bc68/rest_framework/exceptions.py#L99

Upvotes: 9

tombreit
tombreit

Reputation: 1369

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),
]

Upvotes: 2

agconti
agconti

Reputation: 18103

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()

Upvotes: 68

Related Questions