Skip to content

Commit

Permalink
Add error annotation (#27)
Browse files Browse the repository at this point in the history
# 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]>
  • Loading branch information
konraddysput and Konrad Dysput authored Oct 31, 2024
1 parent a33bf05 commit c2b2016
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
13 changes: 12 additions & 1 deletion backtracepython/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import threading
import time
import uuid
import traceback

from backtracepython.attributes.attribute_manager import attribute_manager

Expand Down Expand Up @@ -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
Expand All @@ -63,6 +65,15 @@ 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())

Expand Down
14 changes: 14 additions & 0 deletions tests/test_report_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,17 @@ 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

0 comments on commit c2b2016

Please sign in to comment.