Skip to content

Commit f5cd47f

Browse files
authored
fix: Capture body of Django Rest Framework apps (getsentry#323)
* fix: Capture body of Django Rest Framework apps * fix: Limit rest_framework tests to a few django versions
1 parent 0ae2c1c commit f5cd47f

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-1
lines changed

sentry_sdk/integrations/django/__init__.py

+6
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,12 @@ def files(self):
249249
def size_of_file(self, file):
250250
return file.size
251251

252+
def parsed_body(self):
253+
try:
254+
return self.request.data
255+
except AttributeError:
256+
return RequestExtractor.parsed_body(self)
257+
252258

253259
def _set_user_info(request, event):
254260
# type: (WSGIRequest, Dict[str, Any]) -> None

tests/integrations/django/myapp/urls.py

+8
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,13 @@
3232
path("template-exc", views.template_exc, name="template_exc"),
3333
]
3434

35+
36+
try:
37+
urlpatterns.append(
38+
path("rest-framework-exc", views.rest_framework_exc, name="rest_framework_exc")
39+
)
40+
except AttributeError:
41+
pass
42+
3543
handler500 = views.handler500
3644
handler404 = views.handler404

tests/integrations/django/myapp/views.py

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44
from django.shortcuts import render
55
from django.views.generic import ListView
66

7+
try:
8+
from rest_framework.decorators import api_view
9+
10+
@api_view(["POST"])
11+
def rest_framework_exc(request):
12+
1 / 0
13+
14+
15+
except ImportError:
16+
pass
17+
18+
719
import sentry_sdk
820

921

tests/integrations/django/test_basic.py

+66
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
import json
23

34
from werkzeug.test import Client
45
from django.contrib.auth.models import User
@@ -389,3 +390,68 @@ def test_template_exception(sentry_init, client, capture_events):
389390
(None, None),
390391
(u"invalid_block_tag", u"django.template.base"),
391392
]
393+
394+
395+
@pytest.mark.parametrize(
396+
"type,event_request",
397+
[
398+
[
399+
"json",
400+
{
401+
"cookies": {},
402+
"data": {"foo": "bar"},
403+
"env": {"SERVER_NAME": "localhost", "SERVER_PORT": "80"},
404+
"headers": {
405+
"Content-Length": "14",
406+
"Content-Type": "application/json",
407+
"Host": "localhost",
408+
},
409+
"method": "POST",
410+
"query_string": "",
411+
"url": "http://localhost/rest-framework-exc",
412+
},
413+
],
414+
[
415+
"formdata",
416+
{
417+
"cookies": {},
418+
"data": {"foo": "bar"},
419+
"env": {"SERVER_NAME": "localhost", "SERVER_PORT": "80"},
420+
"headers": {
421+
"Content-Length": "7",
422+
"Content-Type": "application/x-www-form-urlencoded",
423+
"Host": "localhost",
424+
},
425+
"method": "POST",
426+
"query_string": "",
427+
"url": "http://localhost/rest-framework-exc",
428+
},
429+
],
430+
],
431+
)
432+
def test_rest_framework_basic(
433+
sentry_init, client, capture_events, capture_exceptions, type, event_request
434+
):
435+
pytest.importorskip("rest_framework")
436+
sentry_init(integrations=[DjangoIntegration()], send_default_pii=True)
437+
exceptions = capture_exceptions()
438+
events = capture_events()
439+
440+
if type == "json":
441+
client.post(
442+
reverse("rest_framework_exc"),
443+
data=json.dumps({"foo": "bar"}),
444+
content_type="application/json",
445+
)
446+
elif type == "formdata":
447+
client.post(reverse("rest_framework_exc"), data={"foo": "bar"})
448+
else:
449+
assert False
450+
451+
error, = exceptions
452+
assert isinstance(error, ZeroDivisionError)
453+
454+
event, = events
455+
assert event["exception"]["values"][0]["mechanism"]["type"] == "django"
456+
457+
assert event["request"] == event_request

tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ envlist =
4141
deps =
4242
-r test-requirements.txt
4343

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

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

0 commit comments

Comments
 (0)