Skip to content

Commit

Permalink
fix(errors): Make sure project root exists to judge in app frames
Browse files Browse the repository at this point in the history
  • Loading branch information
neilkakkar committed Sep 16, 2024
1 parent 1521621 commit 336f1f2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 2 additions & 0 deletions posthog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
# Currently alpha, use at your own risk
enable_exception_autocapture = False # type: bool
exception_autocapture_integrations = [] # type: List[Integrations]
# Used to determine in app paths for exception autocapture. Defaults to the current working directory
project_root = None # type: Optional[str]

default_client = None # type: Optional[Client]

Expand Down
18 changes: 15 additions & 3 deletions posthog/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import atexit
import logging
import numbers
import os
import sys
from datetime import datetime, timedelta
from uuid import UUID
Expand Down Expand Up @@ -56,6 +57,7 @@ def __init__(
feature_flags_request_timeout_seconds=3,
enable_exception_autocapture=False,
exception_autocapture_integrations=None,
project_root=None,
):
self.queue = queue.Queue(max_queue_size)

Expand Down Expand Up @@ -87,6 +89,14 @@ def __init__(
self.enable_exception_autocapture = enable_exception_autocapture
self.exception_autocapture_integrations = exception_autocapture_integrations
self.exception_capture = None

if project_root is None:
try:
project_root = os.getcwd()
except Exception as e:
project_root = None

self.project_root = project_root

# personal_api_key: This should be a generated Personal API Key, private
self.personal_api_key = personal_api_key
Expand Down Expand Up @@ -382,13 +392,15 @@ def capture_exception(
"exception": {
"values": all_exceptions_with_trace,
},
}
},
project_root=self.project_root,
)
print('prject root: ', self.project_root)
all_exceptions_with_trace_and_in_app = event["exception"]["values"]

properties = {
"$exception_type": all_exceptions_with_trace_and_in_app[0].get("type"),
"$exception_message": all_exceptions_with_trace_and_in_app[0].get("value"),
"$exception_type": all_exceptions_with_trace_and_in_app[-1].get("type"),
"$exception_message": all_exceptions_with_trace_and_in_app[-1].get("value"),
"$exception_list": all_exceptions_with_trace_and_in_app,
"$exception_personURL": f"{remove_trailing_slash(self.raw_host)}/project/{self.api_key}/person/{distinct_id}",
**properties,
Expand Down
4 changes: 4 additions & 0 deletions posthog/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ def test_basic_capture_exception_with_no_exception_given(self):
self.assertEqual(
capture_call[2]["$exception_list"][0]["stacktrace"]["frames"][0]["module"], "posthog.test.test_client"
)
self.assertEqual(
capture_call[2]["$exception_list"][0]["stacktrace"]["frames"][0]["in_app"], True
)


def test_basic_capture_exception_with_no_exception_happening(self):

Expand Down

0 comments on commit 336f1f2

Please sign in to comment.