Skip to content
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
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# 5.1.0
# 5.2.0 - 2025-06-19

- feat: construct artificial stack traces if no traceback is available on a captured exception

## 5.1.0 - 2025-06-18

- feat: session and distinct ID's can now be associated with contexts, and are used as such
- feat: django http request middleware
Expand Down
30 changes: 30 additions & 0 deletions posthog/exception_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import re
import sys
import types
from datetime import datetime
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -809,6 +810,10 @@ def exc_info_from_error(error):
if isinstance(error, tuple) and len(error) == 3:
exc_type, exc_value, tb = error
elif isinstance(error, BaseException):
try:
construct_artificial_traceback(error)
except Exception:
pass
tb = getattr(error, "__traceback__", None)
if tb is not None:
exc_type = type(error)
Expand All @@ -833,6 +838,31 @@ def exc_info_from_error(error):
return exc_info


def construct_artificial_traceback(e):
# type: (BaseException) -> None
if getattr(e, "__traceback__", None) is not None:
return

depth = 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we start the depth at 1 to remove the capture_exception frame?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The one you see in the screenshot is actually the wrapper you added in posthog (there are 5 or 6 frames beneath this one, but they're correctly marked not-in-app, and I think it's fine to leave them)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would actually love to get rid of that wrapper soon. Think it's less necessary now that we have all the context stuff

frames = []
while True:
try:
frame = sys._getframe(depth)
depth += 1
except ValueError:
break

frames.append(frame)

frames.reverse()

tb = None
for frame in frames:
tb = types.TracebackType(tb, frame, frame.f_lasti, frame.f_lineno)

setattr(e, "__traceback__", tb)


def event_from_exception(
exc_info, # type: Union[BaseException, ExcInfo]
client_options=None, # type: Optional[Dict[str, Any]]
Expand Down
64 changes: 0 additions & 64 deletions posthog/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,6 @@ def test_basic_capture_exception(self):
capture_call = patch_capture.call_args[0]
self.assertEqual(capture_call[0], "distinct_id")
self.assertEqual(capture_call[1], "$exception")
self.assertEqual(
capture_call[2],
{
"$exception_type": "Exception",
"$exception_message": "test exception",
"$exception_list": [
{
"mechanism": {"type": "generic", "handled": True},
"module": None,
"type": "Exception",
"value": "test exception",
}
],
"$exception_personURL": "https://us.i.posthog.com/project/random_key/person/distinct_id",
},
)

def test_basic_capture_exception_with_distinct_id(self):
with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
Expand All @@ -144,22 +128,6 @@ def test_basic_capture_exception_with_distinct_id(self):
capture_call = patch_capture.call_args[0]
self.assertEqual(capture_call[0], "distinct_id")
self.assertEqual(capture_call[1], "$exception")
self.assertEqual(
capture_call[2],
{
"$exception_type": "Exception",
"$exception_message": "test exception",
"$exception_list": [
{
"mechanism": {"type": "generic", "handled": True},
"module": None,
"type": "Exception",
"value": "test exception",
}
],
"$exception_personURL": "https://us.i.posthog.com/project/random_key/person/distinct_id",
},
)

def test_basic_capture_exception_with_correct_host_generation(self):
with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
Expand All @@ -173,22 +141,6 @@ def test_basic_capture_exception_with_correct_host_generation(self):
capture_call = patch_capture.call_args[0]
self.assertEqual(capture_call[0], "distinct_id")
self.assertEqual(capture_call[1], "$exception")
self.assertEqual(
capture_call[2],
{
"$exception_type": "Exception",
"$exception_message": "test exception",
"$exception_list": [
{
"mechanism": {"type": "generic", "handled": True},
"module": None,
"type": "Exception",
"value": "test exception",
}
],
"$exception_personURL": "https://aloha.com/project/random_key/person/distinct_id",
},
)

def test_basic_capture_exception_with_correct_host_generation_for_server_hosts(
self,
Expand All @@ -206,22 +158,6 @@ def test_basic_capture_exception_with_correct_host_generation_for_server_hosts(
capture_call = patch_capture.call_args[0]
self.assertEqual(capture_call[0], "distinct_id")
self.assertEqual(capture_call[1], "$exception")
self.assertEqual(
capture_call[2],
{
"$exception_type": "Exception",
"$exception_message": "test exception",
"$exception_list": [
{
"mechanism": {"type": "generic", "handled": True},
"module": None,
"type": "Exception",
"value": "test exception",
}
],
"$exception_personURL": "https://app.posthog.com/project/random_key/person/distinct_id",
},
)

def test_basic_capture_exception_with_no_exception_given(self):
with mock.patch.object(Client, "capture", return_value=None) as patch_capture:
Expand Down
2 changes: 1 addition & 1 deletion posthog/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = "5.1.0"
VERSION = "5.2.0"

if __name__ == "__main__":
print(VERSION, end="") # noqa: T201
Loading