feat: standardize error handling patterns

This commit is contained in:
2025-12-15 21:01:09 +01:00
parent 18ffae7ac8
commit fcb1524806
3 changed files with 12 additions and 12 deletions

View File

@@ -81,7 +81,7 @@ class DownloadManager:
if isinstance(activation_bytes, bytes): if isinstance(activation_bytes, bytes):
return activation_bytes.hex() return activation_bytes.hex()
return str(activation_bytes) return str(activation_bytes)
except Exception: except (OSError, ValueError, KeyError, AttributeError):
return None return None
def _validate_download_url(self, url: str) -> bool: def _validate_download_url(self, url: str) -> bool:
@@ -89,7 +89,7 @@ class DownloadManager:
try: try:
parsed = urlparse(url) parsed = urlparse(url)
return parsed.scheme in ("http", "https") and bool(parsed.netloc) return parsed.scheme in ("http", "https") and bool(parsed.netloc)
except Exception: except (ValueError, AttributeError):
return False return False
def _sanitize_filename(self, filename: str) -> str: def _sanitize_filename(self, filename: str) -> str:
@@ -105,7 +105,7 @@ class DownloadManager:
) )
product = product_info.get("product", {}) product = product_info.get("product", {})
return product.get("title") or "Unknown Title" return product.get("title") or "Unknown Title"
except Exception: except (OSError, ValueError, KeyError):
return None return None
def _get_download_link( def _get_download_link(
@@ -141,7 +141,7 @@ class DownloadManager:
if notify: if notify:
notify(f"Download-link request failed: {exc!s}") notify(f"Download-link request failed: {exc!s}")
return None return None
except Exception as exc: except (OSError, ValueError, KeyError, AttributeError) as exc:
if notify: if notify:
notify(f"Download-link error: {exc!s}") notify(f"Download-link error: {exc!s}")
return None return None
@@ -187,7 +187,7 @@ class DownloadManager:
except OSError: except OSError:
pass pass
return None return None
except Exception as exc: except (OSError, ValueError, KeyError) as exc:
if notify: if notify:
notify(f"Download error: {exc!s}") notify(f"Download error: {exc!s}")
try: try:

View File

@@ -157,7 +157,7 @@ class LibraryClient:
return float(position_ms) / 1000.0 return float(position_ms) / 1000.0
return None return None
except Exception: except (OSError, ValueError, KeyError):
return None return None
def _get_content_reference(self, asin: str) -> dict | None: def _get_content_reference(self, asin: str) -> dict | None:
@@ -172,7 +172,7 @@ class LibraryClient:
if isinstance(content_reference, dict): if isinstance(content_reference, dict):
return content_reference return content_reference
return None return None
except Exception: except (OSError, ValueError, KeyError):
return None return None
def save_last_position(self, asin: str, position_seconds: float) -> bool: def save_last_position(self, asin: str, position_seconds: float) -> bool:
@@ -203,7 +203,7 @@ class LibraryClient:
body=body, body=body,
) )
return True return True
except Exception: except (OSError, ValueError, KeyError):
return False return False
@staticmethod @staticmethod

View File

@@ -93,7 +93,7 @@ class PlaybackController:
notify(f"Playing: {path.name}") notify(f"Playing: {path.name}")
return True return True
except Exception as exc: except (OSError, ValueError, subprocess.SubprocessError) as exc:
notify(f"Error starting playback: {exc}") notify(f"Error starting playback: {exc}")
return False return False
@@ -202,7 +202,7 @@ class PlaybackController:
self.notify("Process no longer exists") self.notify("Process no longer exists")
except PermissionError: except PermissionError:
self.notify(f"Permission denied: cannot {action} playback") self.notify(f"Permission denied: cannot {action} playback")
except Exception as exc: except (OSError, ValueError) as exc:
self.notify(f"Error {action}ing playback: {exc}") self.notify(f"Error {action}ing playback: {exc}")
def is_alive(self) -> bool: def is_alive(self) -> bool:
@@ -245,7 +245,7 @@ class PlaybackController:
start_position = last_position start_position = last_position
notify( notify(
f"Resuming from {LibraryClient.format_time(start_position)}") f"Resuming from {LibraryClient.format_time(start_position)}")
except Exception: except (OSError, ValueError, KeyError):
pass pass
notify(f"Starting playback of {local_path.name}...") notify(f"Starting playback of {local_path.name}...")
@@ -510,7 +510,7 @@ class PlaybackController:
try: try:
self.library_client.save_last_position( self.library_client.save_last_position(
self.current_asin, current_position) self.current_asin, current_position)
except Exception: except (OSError, ValueError, KeyError):
pass pass
def update_position_if_needed(self) -> None: def update_position_if_needed(self) -> None: