Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error annotation #27

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading