Skip to content

Commit 95ad1bf

Browse files
authored
Merge pull request #145 from chanchiem/django-exception
Fix exception processing in Django running in Lambda #86
2 parents 744360d + ea97291 commit 95ad1bf

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

aws_xray_sdk/ext/django/middleware.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ def process_exception(self, request, exception):
9595
Add exception information and fault flag to the
9696
current segment.
9797
"""
98-
segment = xray_recorder.current_segment()
98+
if self.in_lambda_ctx:
99+
segment = xray_recorder.current_subsegment()
100+
else:
101+
segment = xray_recorder.current_segment()
99102
segment.put_http_meta(http.STATUS, 500)
100103

101104
stack = stacktrace.get_stacktrace(limit=xray_recorder._max_trace_back)

tests/ext/django/test_middleware.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,25 @@ def test_lambda_serverless(self):
133133
segment = new_recorder.emitter.pop()
134134
assert not segment
135135

136+
# Test Fault in Lambda
137+
url = reverse('500fault')
138+
try:
139+
self.client.get(url)
140+
except Exception:
141+
pass
142+
segment = xray_recorder.emitter.pop()
143+
assert segment.fault
144+
145+
request = segment.http['request']
146+
response = segment.http['response']
147+
148+
assert request['method'] == 'GET'
149+
assert request['client_ip'] == '127.0.0.1'
150+
assert response['status'] == 500
151+
152+
exception = segment.cause['exceptions'][0]
153+
assert exception.type == 'KeyError'
154+
136155
def test_lambda_default_ctx(self):
137156
# Track to make sure that Django will default to generating segments if context is not the lambda context
138157
url = reverse('200ok')

0 commit comments

Comments
 (0)