Skip to content
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
6 changes: 6 additions & 0 deletions sentry_sdk/integrations/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,12 @@ def files(self):
def size_of_file(self, file):
return file.size

def parsed_body(self):
try:
return self.request.data
except AttributeError:
return RequestExtractor.parsed_body(self)


def _set_user_info(request, event):
# type: (WSGIRequest, Dict[str, Any]) -> None
Expand Down
8 changes: 8 additions & 0 deletions tests/integrations/django/myapp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,13 @@
path("template-exc", views.template_exc, name="template_exc"),
]


try:
urlpatterns.append(
path("rest-framework-exc", views.rest_framework_exc, name="rest_framework_exc")
)
except AttributeError:
pass

handler500 = views.handler500
handler404 = views.handler404
12 changes: 12 additions & 0 deletions tests/integrations/django/myapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
from django.shortcuts import render
from django.views.generic import ListView

try:
from rest_framework.decorators import api_view

@api_view(["POST"])
def rest_framework_exc(request):
1 / 0


except ImportError:
pass


import sentry_sdk


Expand Down
66 changes: 66 additions & 0 deletions tests/integrations/django/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import json

from werkzeug.test import Client
from django.contrib.auth.models import User
Expand Down Expand Up @@ -389,3 +390,68 @@ def test_template_exception(sentry_init, client, capture_events):
(None, None),
(u"invalid_block_tag", u"django.template.base"),
]


@pytest.mark.parametrize(
"type,event_request",
[
[
"json",
{
"cookies": {},
"data": {"foo": "bar"},
"env": {"SERVER_NAME": "localhost", "SERVER_PORT": "80"},
"headers": {
"Content-Length": "14",
"Content-Type": "application/json",
"Host": "localhost",
},
"method": "POST",
"query_string": "",
"url": "http://localhost/rest-framework-exc",
},
],
[
"formdata",
{
"cookies": {},
"data": {"foo": "bar"},
"env": {"SERVER_NAME": "localhost", "SERVER_PORT": "80"},
"headers": {
"Content-Length": "7",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "localhost",
},
"method": "POST",
"query_string": "",
"url": "http://localhost/rest-framework-exc",
},
],
],
)
def test_rest_framework_basic(
sentry_init, client, capture_events, capture_exceptions, type, event_request
):
pytest.importorskip("rest_framework")
sentry_init(integrations=[DjangoIntegration()], send_default_pii=True)
exceptions = capture_exceptions()
events = capture_events()

if type == "json":
client.post(
reverse("rest_framework_exc"),
data=json.dumps({"foo": "bar"}),
content_type="application/json",
)
elif type == "formdata":
client.post(reverse("rest_framework_exc"), data={"foo": "bar"})
else:
assert False

error, = exceptions
assert isinstance(error, ZeroDivisionError)

event, = events
assert event["exception"]["values"][0]["mechanism"]["type"] == "django"

assert event["request"] == event_request
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ envlist =
deps =
-r test-requirements.txt

py{2.7,3.4,3.5,3.6,3.7}-django: psycopg2>=2.7.5
django-{1.11,2.0,2.1}: djangorestframework>=3.0.0,<4.0.0

django-{1.6,1.7,1.8}: pytest-django<3.0
django-{1.9,1.10,1.11,2.0,2.1,dev}: pytest-django>=3.0
Expand Down