From 44202a47fcc9ec6c6a5805b9a02529f71a59d2a4 Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Tue, 29 Oct 2024 17:57:54 +0100 Subject: [PATCH 1/3] Add error annotation --- backtracepython/report.py | 10 +++++++++- tests/test_report_attributes.py | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/backtracepython/report.py b/backtracepython/report.py index 28fc747..c0c5958 100644 --- a/backtracepython/report.py +++ b/backtracepython/report.py @@ -3,6 +3,7 @@ import threading import time import uuid +import traceback from backtracepython.attributes.attribute_manager import attribute_manager @@ -39,7 +40,8 @@ def __init__(self): } def set_exception(self, garbage, ex_value, ex_traceback): - self.report["classifiers"] = [ex_value.__class__.__name__] + exception_classifier = ex_value.__class__.__name__ + self.report["classifiers"] = [exception_classifier] self.report["attributes"]["error.message"] = str(ex_value) # reset faulting thread id and make sure the faulting thread is not listed twice @@ -63,6 +65,12 @@ def set_exception(self, garbage, ex_value, ex_traceback): self.faulting_thread_id = fault_thread_id self.report["mainThread"] = self.faulting_thread_id + self.set_annotation("Exception", { + "type": exception_classifier, + "message": str(ex_value), + "traceback": traceback.format_tb(ex_traceback) + }) + def capture_last_exception(self): self.set_exception(*sys.exc_info()) diff --git a/tests/test_report_attributes.py b/tests/test_report_attributes.py index e277c26..76888ea 100644 --- a/tests/test_report_attributes.py +++ b/tests/test_report_attributes.py @@ -85,3 +85,18 @@ def test_override_client_annotation(): new_report.set_annotation(annotation_name, override_report_annotation) report_annotation = new_report.get_annotations() assert report_annotation[annotation_name] == override_report_annotation + + +def test_set_exception_annotation(): + + def open_file(name): + open(name).read() + + try: + open_file("not existing file") + except: + report = BacktraceReport() + report.capture_last_exception() + annotations = report.get_annotations() + assert annotations["Exception"] is not None + \ No newline at end of file From a6e0dd63d9fd35ab2ee6d9b5adc01e670963e5d4 Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Tue, 29 Oct 2024 17:59:25 +0100 Subject: [PATCH 2/3] EOL --- tests/test_report_attributes.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_report_attributes.py b/tests/test_report_attributes.py index 76888ea..92aa632 100644 --- a/tests/test_report_attributes.py +++ b/tests/test_report_attributes.py @@ -99,4 +99,3 @@ def open_file(name): report.capture_last_exception() annotations = report.get_annotations() assert annotations["Exception"] is not None - \ No newline at end of file From a7655560479a2f776fc7c083b4cf27543bc43da9 Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Tue, 29 Oct 2024 18:02:51 +0100 Subject: [PATCH 3/3] Formatting --- backtracepython/report.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/backtracepython/report.py b/backtracepython/report.py index c0c5958..1c5a9ee 100644 --- a/backtracepython/report.py +++ b/backtracepython/report.py @@ -65,11 +65,14 @@ def set_exception(self, garbage, ex_value, ex_traceback): self.faulting_thread_id = fault_thread_id self.report["mainThread"] = self.faulting_thread_id - self.set_annotation("Exception", { - "type": exception_classifier, - "message": str(ex_value), - "traceback": traceback.format_tb(ex_traceback) - }) + self.set_annotation( + "Exception", + { + "type": exception_classifier, + "message": str(ex_value), + "traceback": traceback.format_tb(ex_traceback), + }, + ) def capture_last_exception(self): self.set_exception(*sys.exc_info())