From f70376816101e4417c1d7ce773c01c5568d0d108 Mon Sep 17 00:00:00 2001 From: Evan Pratten Date: Tue, 20 Feb 2024 19:16:11 -0500 Subject: [PATCH] better error handling --- scripts/pwnagotchi-watch | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/scripts/pwnagotchi-watch b/scripts/pwnagotchi-watch index 67ef62f..2932e54 100755 --- a/scripts/pwnagotchi-watch +++ b/scripts/pwnagotchi-watch @@ -40,11 +40,24 @@ def main() -> int: # Continuously fetch the pwnagotchi screen while True: + + # If the window is closed, exit + for event in pygame.event.get(): + if event.type == pygame.QUIT: + return 0 + + # Attempt to fetch the pwnagotchi screen try: # Fetch the pwnagotchi screen logger.debug(f"Fetching pwnagotchi screen from {args.host}:{args.port}") - response = requests.get(f"http://{args.host}:{args.port}/ui", auth=(args.username, args.password)) - response.raise_for_status() + try: + response = requests.get(f"http://{args.host}:{args.port}/ui", auth=(args.username, args.password), timeout=1) + response.raise_for_status() + except Exception as e: + logger.error(f"Failed to connect to pwnagotchi at {args.host}:{args.port}") + logger.error(f"Error: {e}") + time.sleep(args.refresh_rate) + continue screen_data = response.content # Draw the screen to the window @@ -64,10 +77,6 @@ def main() -> int: time.sleep(args.refresh_rate) - # If the window is closed, exit - for event in pygame.event.get(): - if event.type == pygame.QUIT: - return 0 return 0