Skip to content

Commit 782e560

Browse files
committed
fix deletes and add email to errors
1 parent 2269c48 commit 782e560

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

backend/app/fastapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE
9797

9898
@app.exception_handler(Exception)
9999
async def unhandled_exception_handler(request: Request, exc: Exception):
100-
posthog_capture_exception(exc)
100+
posthog_capture_exception(exc, request)
101101
return JSONResponse(status_code=500, content={"detail": str(exc)})
102102

103103
if __name__ == '__main__':

backend/app/models/frame.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ async def delete_frame(db: Session, redis: Redis, frame_id: int):
194194
db.query(Log).filter_by(frame_id=frame_id).delete()
195195
from .metrics import Metrics
196196
db.query(Metrics).filter_by(frame_id=frame_id).delete()
197+
from .scene_image import SceneImage
198+
db.query(SceneImage).filter_by(frame_id=frame_id).delete()
197199

198200
cache_key = f'frame:{frame.frame_host}:{frame.frame_port}:image'
199201
await redis.delete(cache_key)

backend/app/utils/posthog.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from typing import Optional, Any
2+
from fastapi import Request
3+
from jose import jwt, JWTError
14
from posthog import Posthog
25

36
from app.config import config
@@ -14,12 +17,31 @@ def initialize_posthog() -> None:
1417
posthog_client = Posthog(project_api_key=token, host=host)
1518

1619

17-
def capture_exception(exc: Exception) -> None:
20+
def _get_email_from_request(request: Request) -> Optional[str]:
21+
auth = request.headers.get("Authorization")
22+
if not auth or not auth.startswith("Bearer ") or not config.SECRET_KEY:
23+
return None
24+
token = auth.split(" ", 1)[1]
25+
try:
26+
payload = jwt.decode(token, config.SECRET_KEY, algorithms=["HS256"])
27+
except JWTError:
28+
return None
29+
return payload.get("sub")
30+
31+
32+
def capture_exception(exc: Exception, request: Optional[Request] = None) -> None:
1833
"""Capture an exception with PostHog if initialized."""
1934
if posthog_client is None:
2035
return
36+
37+
kwargs: dict[str, Any] = {}
38+
if request is not None:
39+
email = _get_email_from_request(request)
40+
if email:
41+
kwargs["distinct_id"] = email
42+
kwargs["properties"] = {"email": email} # TODO: decouple
2143
try:
22-
posthog_client.capture_exception(exc)
44+
posthog_client.capture_exception(exc, **kwargs)
2345
except Exception:
2446
# Avoid raising exceptions from PostHog itself
25-
pass
47+
pass

0 commit comments

Comments
 (0)