Skip to content

Commit

Permalink
Add better error handling for notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
gizmo385 committed Dec 17, 2024
1 parent b2fca4a commit 0c31317
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lazy_github/lib/github/notifications.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import json

from lazy_github.lib.github.backends.cli import run_gh_cli_command
from lazy_github.lib.logging import lg
from lazy_github.models.github import Notification

NOTIFICATIONS_PAGE_COUNT = 30


async def fetch_notifications(all: bool) -> list[Notification]:
"""Fetches notifications on GitHub. If all=True, then previously read notifications will also be returned"""
result = await run_gh_cli_command(["api", f"/notifications?all={str(all).lower()}"])
notifications: list[Notification] = []
if result.stdout:
parsed = json.loads(result.stdout)
notifications = [Notification(**n) for n in parsed]
try:
result = await run_gh_cli_command(["api", f"/notifications?all={str(all).lower()}"])
if result.stdout:
parsed = json.loads(result.stdout)
notifications = [Notification(**n) for n in parsed]
except Exception:
lg.exception("Failed to retrieve notifications from the Github API")
return notifications


async def mark_notification_as_read(notification: Notification) -> None:
await run_gh_cli_command(["--method", "PATCH", "api", f"/notifications/threads/{notification.id}"])
try:
await run_gh_cli_command(["--method", "PATCH", "api", f"/notifications/threads/{notification.id}"])
except Exception:
lg.exception("Failed to mark notification as read")


async def unread_notification_count() -> int:
Expand Down

0 comments on commit 0c31317

Please sign in to comment.