From 9640a806a36095c7539b66feb0869f99ef559017 Mon Sep 17 00:00:00 2001 From: Arslaan Pathan Date: Mon, 5 Oct 2026 23:51:14 +1300 Subject: error handling --- main.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'main.py') diff --git a/main.py b/main.py index 0fca35e..489a2f6 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,6 @@ from evdev import InputDevice, list_devices, ecodes from urllib.request import urlopen +from urllib.error import HTTPError, URLError SCANCODES = {0x11A: "up", 0x148: "down", 0x147: "left", 0x107: "right", 0x106: "ok", 0x141: "play/pause", 0x11c: "power", 0x103: "vol+", 0x142: "vol-"} @@ -7,8 +8,19 @@ device = None last = 0x00 def homelab_request(request: str): - with urlopen(f"http://192.168.1.96/automate/{request}") as resp: - print(f"homelab request {request} response {resp.status}") + url = f"http://192.168.1.96/automate/{request}" + + try: + with urlopen(url, timeout=5) as resp: + print(f"homelab request {request} response {resp.status}") + print(resp.read().decode()) + + except HTTPError as e: + print(f"homelab request {request} response {e.code}") + print(e.read().decode()) + + except URLError as e: + print(f"homelab request failed: {e.reason}") def do_action(action: str): if action == "ok": -- cgit v1.2.3