Skip to content

Commit

Permalink
Add _handle_exceptions wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
jake158 committed May 3, 2024
1 parent 6013ab3 commit 69970b0
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/logic/richpresence.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ def __init__(self):
self.connect()
self.idling_state()

def _handle_exceptions(func):
def wrapper(self, *args, **kwargs):
if not self.connected:
return
try:
func(self, *args, **kwargs)
except Exception as e:
print(f"Error updating Rich Presence: {e}")
self.connected = False
return wrapper

def connect(self):
try:
super().connect()
Expand Down Expand Up @@ -41,18 +52,22 @@ def format_time(self, seconds_studied):
else:
return f"{round(total_hours, 1) if total_hours % 1 != 0 else int(total_hours)} hours"

@_handle_exceptions
def idling_state(self):
self.update(state="Idling", details=None, start=self.launch_time, large_image="graytomato",
large_text="github.com/freeram/pomodoro-discord")

@_handle_exceptions
def running_state(self, session, start_time, end_time):
self.update(state=f"Session {session}", details="Studying", start=start_time,
end=end_time, large_image="tomato", large_text="github.com/freeram/pomodoro-discord")

@_handle_exceptions
def paused_state(self, start_time):
self.update(state="Paused", details=None, start=start_time, large_image="graytomato",
large_text="github.com/freeram/pomodoro-discord")

@_handle_exceptions
def break_state(self, seconds_studied, start_time, end_time):
self.update(state=f"Time studied: {self.format_time(seconds_studied)}", details="On break", start=start_time,
end=end_time, large_image="greentomato", large_text="github.com/freeram/pomodoro-discord")

0 comments on commit 69970b0

Please sign in to comment.