Skip to content

Commit f21522d

Browse files
committed
Capture exceptions to Sentry instead of printing
1 parent 9fedea1 commit f21522d

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

src/psrt_ghsa_bot/_sentry_monitoring.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def init_sentry() -> None:
2626
)
2727

2828

29-
def capture_checkin(monitor_slug, status, duration=None, check_in_id=None):
29+
def capture_checkin(monitor_slug, status, duration=None, check_in_id=None) -> None | str:
3030
"""Capture a Sentry cron check-in."""
3131
if not os.environ.get("SENTRY_DSN"):
3232
return None
@@ -40,3 +40,10 @@ def capture_checkin(monitor_slug, status, duration=None, check_in_id=None):
4040
)
4141
except ImportError, AttributeError:
4242
return None
43+
44+
45+
def capture_exception() -> None:
46+
"""Capture an exception if Sentry is active."""
47+
if not os.environ.get("SENTRY_DSN"):
48+
return
49+
sentry_sdk.capture_exception()

src/psrt_ghsa_bot/app.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
STATUS_IN_PROGRESS,
2121
STATUS_OK,
2222
capture_checkin,
23+
capture_exception,
2324
init_sentry,
2425
)
2526

@@ -89,7 +90,9 @@ def get_repository_advisories(
8990
# 404 means no advisories or no access - that's okay
9091
if e.response.status_code == 404:
9192
return
92-
raise
93+
# Capture the original exception in Sentry (private)
94+
# and emit a sanitized public exception.
95+
capture_exception()
9396

9497

9598
def github_client_request(client: typing.Any, method: str, url: str, params: dict[str, str | int]) -> typing.Any:
@@ -161,9 +164,11 @@ def apply_to_repo(github: GitHub, owner: str, repo: str, cve_api: CveApi) -> Non
161164
repo=repo,
162165
ghsa_id=ghsa_id,
163166
)
164-
except RequestFailed as e:
165-
print(f" ⚠️ Error creating private fork: {e.response.json()}")
166-
raise e
167+
except RequestFailed:
168+
# Capture the original exception in Sentry (private)
169+
# and emit a sanitized public exception.
170+
capture_exception()
171+
raise RuntimeError("Request to create a private fork failed") from None
167172

168173
# Advisories that are in the 'draft' state without a CVE ID
169174
# should have one allocated by the PSF CVE Numbering Authority.
@@ -187,9 +192,11 @@ def apply_to_repo(github: GitHub, owner: str, repo: str, cve_api: CveApi) -> Non
187192
ghsa_id=ghsa_id,
188193
data=patch_data,
189194
)
190-
except RequestFailed as e:
191-
print(f" ⚠️ Error updating advisory: {e.response.json()}")
192-
raise e
195+
except RequestFailed:
196+
# Capture the original exception in Sentry (private)
197+
# and emit a sanitized public exception.
198+
capture_exception()
199+
raise RuntimeError("Request to update advisory failed") from None
193200
print(" 💾 Updated advisory")
194201
else:
195202
print(" ⏭️ No updates needed")

0 commit comments

Comments
 (0)