Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 657 Vote(s) - 3.58 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I trigger a 500 error in Django?

#1
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.
Reply

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

Reply

#3
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][1] that you might raise.


[1]:

[To see links please register here]

Reply

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

```python3
# django urls.py

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

urlpatterns = [
path('500/', server_error),
]
```
Reply

#5
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:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through