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

View File

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

View File

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