Skip to content

Commit d053f04

Browse files
committed
log a debug message if accessing request.body fails (#380)
closes #380
1 parent 9de1b97 commit d053f04

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

elasticapm/contrib/django/client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ def get_data_from_request(self, request, capture_body=False):
115115
else:
116116
try:
117117
data = request.body
118-
except Exception:
118+
except Exception as e:
119+
self.logger.debug("Can't capture request body: %s", compat.text_type(e))
119120
data = "<unavailable>"
120121

121122
result["body"] = data if (capture_body or not data) else "[REDACTED]"

tests/contrib/django/django_tests.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,19 @@ def test_post_raw_data(django_elasticapm_client):
550550
assert request["body"] == "[REDACTED]"
551551

552552

553+
def test_post_read_error_logging(django_elasticapm_client, caplog, rf):
554+
request = rf.post("/test", data="{}", content_type="application/json")
555+
556+
def read():
557+
raise IOError("foobar")
558+
559+
request.read = read
560+
with caplog.at_level(logging.DEBUG):
561+
django_elasticapm_client.get_data_from_request(request, capture_body=True)
562+
record = caplog.records[0]
563+
assert record.message == "Can't capture request body: foobar"
564+
565+
553566
@pytest.mark.skipif(django.VERSION < (1, 9), reason="get-raw-uri-not-available")
554567
def test_disallowed_hosts_error_django_19(django_elasticapm_client):
555568
request = WSGIRequest(

0 commit comments

Comments
 (0)