Coverage for app/backend/src/couchers/sentry.py: 87%

15 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-20 06:55 +0000

1from datetime import UTC, datetime, timedelta 

2from urllib.parse import urlencode 

3 

4import sentry_sdk 

5 

6from couchers.config import config 

7 

8 

9def report_error(exception: Exception) -> None: 

10 """Report an exception to Sentry.""" 

11 sentry_sdk.capture_exception(exception) 

12 

13 

14def report_message(message: str) -> None: 

15 """Report an informational message to Sentry.""" 

16 sentry_sdk.capture_message(message) 

17 

18 

19def _dashboard_timestamp(dt: datetime) -> str: 

20 return dt.astimezone(UTC).strftime("%Y-%m-%dT%H:%M:%S") 

21 

22 

23def frontend_user_issues_link(*, user_id: int, reported_at: datetime) -> str: 

24 """Dashboard link to a user's web frontend errors around the time they reported a bug.""" 

25 params = urlencode( 

26 { 

27 "project": config.SENTRY_FRONTEND_PROJECT_ID, 

28 "query": f"user.id:{user_id}", 

29 "start": _dashboard_timestamp(reported_at - timedelta(hours=24)), 

30 "end": _dashboard_timestamp(reported_at + timedelta(hours=1)), 

31 "utc": "true", 

32 } 

33 ) 

34 return f"https://couchers.sentry.io/issues/?{params}" 

35 

36 

37def frontend_replay_link(*, replay_id: str) -> str: 

38 """Dashboard link to a session replay in the web frontend project.""" 

39 return f"https://couchers.sentry.io/replays/{replay_id}/?project={config.SENTRY_FRONTEND_PROJECT_ID}"