Skip to content

Commit c2b2016

Browse files
konraddysputKonrad Dysput
andauthored
Add error annotation (#27)
# Why For debugging purposes and to make sure we can verify everything remotely, it's worth adding an annotation that describes a current exception object captured by us. This pull request adds by default an Exception annotation with the handled exception object. --------- Co-authored-by: Konrad Dysput <[email protected]>
1 parent a33bf05 commit c2b2016

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

backtracepython/report.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import threading
44
import time
55
import uuid
6+
import traceback
67

78
from backtracepython.attributes.attribute_manager import attribute_manager
89

@@ -39,7 +40,8 @@ def __init__(self):
3940
}
4041

4142
def set_exception(self, garbage, ex_value, ex_traceback):
42-
self.report["classifiers"] = [ex_value.__class__.__name__]
43+
exception_classifier = ex_value.__class__.__name__
44+
self.report["classifiers"] = [exception_classifier]
4345
self.report["attributes"]["error.message"] = str(ex_value)
4446

4547
# reset faulting thread id and make sure the faulting thread is not listed twice
@@ -63,6 +65,15 @@ def set_exception(self, garbage, ex_value, ex_traceback):
6365
self.faulting_thread_id = fault_thread_id
6466
self.report["mainThread"] = self.faulting_thread_id
6567

68+
self.set_annotation(
69+
"Exception",
70+
{
71+
"type": exception_classifier,
72+
"message": str(ex_value),
73+
"traceback": traceback.format_tb(ex_traceback),
74+
},
75+
)
76+
6677
def capture_last_exception(self):
6778
self.set_exception(*sys.exc_info())
6879

tests/test_report_attributes.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,17 @@ def test_override_client_annotation():
8585
new_report.set_annotation(annotation_name, override_report_annotation)
8686
report_annotation = new_report.get_annotations()
8787
assert report_annotation[annotation_name] == override_report_annotation
88+
89+
90+
def test_set_exception_annotation():
91+
92+
def open_file(name):
93+
open(name).read()
94+
95+
try:
96+
open_file("not existing file")
97+
except:
98+
report = BacktraceReport()
99+
report.capture_last_exception()
100+
annotations = report.get_annotations()
101+
assert annotations["Exception"] is not None

0 commit comments

Comments
 (0)