From 55f676da16691c7eb23f1ffcb3b589dc3842b274 Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Thu, 13 Nov 2025 14:43:26 -0500 Subject: Replace bare exception catches with specific exceptions - Replace Exception with httpx.HTTPError, httpx.TimeoutException, re.error in extract_og_metadata - Replace Exception with ValueError, KeyError in auth verification - Replace Exception with httpx errors and ValueError in submit_article - Replace Exception with ValueError, KeyError, AttributeError in RSS feed generation - Replace Exception with ValueError, KeyError in cancel/retry/delete job handlers Improves error handling specificity and removes BLE001 linter suppressions. Amp-Thread-ID: https://ampcode.com/threads/T-8edacbeb-b343-49ca-b524-1c999272acb6 Co-authored-by: Amp --- Biz/PodcastItLater/Admin.py | 4 ++-- Biz/PodcastItLater/Web.py | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) (limited to 'Biz/PodcastItLater') diff --git a/Biz/PodcastItLater/Admin.py b/Biz/PodcastItLater/Admin.py index 4920b39..6892234 100644 --- a/Biz/PodcastItLater/Admin.py +++ b/Biz/PodcastItLater/Admin.py @@ -615,7 +615,7 @@ def retry_queue_item(request: Request, job_id: int) -> Response: status_code=200, headers={"HX-Redirect": "/admin"}, ) - except Exception as e: # noqa: BLE001 + except (ValueError, KeyError) as e: return Response( f"Error retrying job: {e!s}", status_code=500, @@ -643,7 +643,7 @@ def delete_queue_item(request: Request, job_id: int) -> Response: status_code=200, headers={"HX-Redirect": "/admin"}, ) - except Exception as e: # noqa: BLE001 + except (ValueError, KeyError) as e: return Response( f"Error deleting job: {e!s}", status_code=500, diff --git a/Biz/PodcastItLater/Web.py b/Biz/PodcastItLater/Web.py index 27b75dd..3524be8 100644 --- a/Biz/PodcastItLater/Web.py +++ b/Biz/PodcastItLater/Web.py @@ -159,7 +159,7 @@ def extract_og_metadata(url: str) -> tuple[str | None, str | None]: if author: author = html_module.unescape(author) - except Exception as e: # noqa: BLE001 + except (httpx.HTTPError, httpx.TimeoutException, re.error) as e: logger.warning("Failed to extract metadata from %s: %s", url, e) return None, None else: @@ -1113,7 +1113,8 @@ def verify_magic_link(request: Request) -> Response: return RedirectResponse("/") - except Exception: # noqa: BLE001 + except (ValueError, KeyError): + # Token is invalid or expired return RedirectResponse("/?error=expired_link") @@ -1288,7 +1289,7 @@ def submit_article(request: Request, data: FormData) -> html.div: # noqa: PLR09 classes=["alert", "alert-success"], ) - except Exception as e: # noqa: BLE001 + except (httpx.HTTPError, httpx.TimeoutException, ValueError) as e: return html.div( html.i(classes=["bi", "bi-exclamation-triangle", "me-2"]), f"Error: {e!s}", @@ -1343,7 +1344,7 @@ def rss_feed(request: Request, token: str) -> Response: # noqa: ARG001 media_type="application/rss+xml; charset=utf-8", ) - except Exception as e: # noqa: BLE001 + except (ValueError, KeyError, AttributeError) as e: return Response(f"Error generating feed: {e}", status_code=500) @@ -1473,7 +1474,7 @@ def cancel_queue_item(request: Request, job_id: int) -> Response: status_code=200, headers={"HX-Trigger": "queue-updated"}, ) - except Exception as e: # noqa: BLE001 + except (ValueError, KeyError) as e: return Response( f"Error cancelling job: {e!s}", status_code=500, -- cgit v1.2.3