Skip to content

Commit e8c60f2

Browse files
srprashTyler Hargraves
authored and
Tyler Hargraves
committed
Handling condition where Entity.cause is not a dict. (aws#267)
* Handling condition where cause is not a dict. Exceptions should be appended not replaced. * Adding more test cases * Minor fixes to some tests * Some type checking in python2 may require to import unicode literals * Checking python version for type comparison
1 parent 338b0e4 commit e8c60f2

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

aws_xray_sdk/core/models/entity.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def add_exception(self, exception, stack, remote=False):
211211
"""
212212
Add an exception to trace entities.
213213
214-
:param Exception exception: the catched exception.
214+
:param Exception exception: the caught exception.
215215
:param list stack: the output from python built-in
216216
`traceback.extract_stack()`.
217217
:param bool remote: If False it means it's a client error
@@ -224,7 +224,16 @@ def add_exception(self, exception, stack, remote=False):
224224
setattr(self, 'cause', getattr(exception, '_cause_id'))
225225
return
226226

227-
exceptions = []
227+
if not isinstance(self.cause, dict):
228+
log.warning("The current cause object is not a dict but an id: {}. Resetting the cause and recording the "
229+
"current exception".format(self.cause))
230+
self.cause = {}
231+
232+
if 'exceptions' in self.cause:
233+
exceptions = self.cause['exceptions']
234+
else:
235+
exceptions = []
236+
228237
exceptions.append(Throwable(exception, stack, remote))
229238

230239
self.cause['exceptions'] = exceptions

tests/test_trace_entities.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- coding: iso-8859-15 -*-
2+
23
import pytest
4+
import sys
35

46
from aws_xray_sdk.core.models.segment import Segment
57
from aws_xray_sdk.core.models.subsegment import Subsegment
@@ -194,3 +196,70 @@ def test_missing_parent_segment():
194196

195197
with pytest.raises(SegmentNotFoundException):
196198
Subsegment('name', 'local', None)
199+
200+
201+
def test_add_exception():
202+
segment = Segment('seg')
203+
exception = Exception("testException")
204+
stack = [['path', 'line', 'label']]
205+
segment.add_exception(exception=exception, stack=stack)
206+
segment.close()
207+
208+
cause = segment.cause
209+
assert 'exceptions' in cause
210+
exceptions = cause['exceptions']
211+
assert len(exceptions) == 1
212+
assert 'working_directory' in cause
213+
exception = exceptions[0]
214+
assert 'testException' == exception.message
215+
expected_stack = [{'path': 'path', 'line': 'line', 'label': 'label'}]
216+
assert expected_stack == exception.stack
217+
218+
219+
def test_add_exception_referencing():
220+
segment = Segment('seg')
221+
subseg = Subsegment('subseg', 'remote', segment)
222+
exception = Exception("testException")
223+
stack = [['path', 'line', 'label']]
224+
subseg.add_exception(exception=exception, stack=stack)
225+
segment.add_exception(exception=exception, stack=stack)
226+
subseg.close()
227+
segment.close()
228+
229+
seg_cause = segment.cause
230+
subseg_cause = subseg.cause
231+
232+
assert isinstance(subseg_cause, dict)
233+
if sys.version_info.major == 2:
234+
assert isinstance(seg_cause, basestring)
235+
else:
236+
assert isinstance(seg_cause, str)
237+
assert seg_cause == subseg_cause['exceptions'][0].id
238+
239+
240+
def test_add_exception_cause_resetting():
241+
segment = Segment('seg')
242+
subseg = Subsegment('subseg', 'remote', segment)
243+
exception = Exception("testException")
244+
stack = [['path', 'line', 'label']]
245+
subseg.add_exception(exception=exception, stack=stack)
246+
segment.add_exception(exception=exception, stack=stack)
247+
248+
segment.add_exception(exception=Exception("newException"), stack=stack)
249+
subseg.close()
250+
segment.close()
251+
252+
seg_cause = segment.cause
253+
assert isinstance(seg_cause, dict)
254+
assert 'newException' == seg_cause['exceptions'][0].message
255+
256+
257+
def test_add_exception_appending_exceptions():
258+
segment = Segment('seg')
259+
stack = [['path', 'line', 'label']]
260+
segment.add_exception(exception=Exception("testException"), stack=stack)
261+
segment.add_exception(exception=Exception("newException"), stack=stack)
262+
segment.close()
263+
264+
assert isinstance(segment.cause, dict)
265+
assert len(segment.cause['exceptions']) == 2

0 commit comments

Comments
 (0)