Skip to content

Commit

Permalink
Merge pull request #145 from chanchiem/django-exception
Browse files Browse the repository at this point in the history
Fix exception processing in Django running in Lambda #86
  • Loading branch information
chanchiem authored Mar 4, 2019
2 parents 744360d + ea97291 commit 95ad1bf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion aws_xray_sdk/ext/django/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ def process_exception(self, request, exception):
Add exception information and fault flag to the
current segment.
"""
segment = xray_recorder.current_segment()
if self.in_lambda_ctx:
segment = xray_recorder.current_subsegment()
else:
segment = xray_recorder.current_segment()
segment.put_http_meta(http.STATUS, 500)

stack = stacktrace.get_stacktrace(limit=xray_recorder._max_trace_back)
Expand Down
19 changes: 19 additions & 0 deletions tests/ext/django/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,25 @@ def test_lambda_serverless(self):
segment = new_recorder.emitter.pop()
assert not segment

# Test Fault in Lambda
url = reverse('500fault')
try:
self.client.get(url)
except Exception:
pass
segment = xray_recorder.emitter.pop()
assert segment.fault

request = segment.http['request']
response = segment.http['response']

assert request['method'] == 'GET'
assert request['client_ip'] == '127.0.0.1'
assert response['status'] == 500

exception = segment.cause['exceptions'][0]
assert exception.type == 'KeyError'

def test_lambda_default_ctx(self):
# Track to make sure that Django will default to generating segments if context is not the lambda context
url = reverse('200ok')
Expand Down

0 comments on commit 95ad1bf

Please sign in to comment.