Skip to content

Commit

Permalink
refactor: reduce duplication in error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
NTFSvolume committed Jan 21, 2025
1 parent b1e950b commit 60fadda
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
3 changes: 2 additions & 1 deletion cyberdrop_dl/clients/download_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ async def _download(
and any(s in content_type.lower() for s in ("html", "text"))
and ext not in FILE_FORMATS["Text"]
):
raise InvalidContentTypeError(message=f"Received '{content_type}', was expecting other")
msg = f"Received '{content_type}', was expecting other"
raise InvalidContentTypeError(message=msg)

if resp.status != HTTPStatus.PARTIAL_CONTENT and media_item.partial_file.is_file():
media_item.partial_file.unlink()
Expand Down
24 changes: 14 additions & 10 deletions cyberdrop_dl/clients/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,24 @@ class PasswordProtectedError(CDLBaseError):
def __init__(self, message: str | None = None, *, origin: ScrapeItem | MediaItem | URL | None = None) -> None:
"""This error will be thrown when a file is password protected."""
ui_message = "Password Protected"
message = message or "File/Folder is password protected"
super().__init__(ui_message, message=message, origin=origin)
msg = message or "File/Folder is password protected"
super().__init__(ui_message, message=msg, origin=origin)


class MaxChildrenError(CDLBaseError):
def __init__(self, message: str | None = None, *, origin: ScrapeItem | MediaItem | URL | None = None) -> None:
"""This error will be thrown when an scrape item reaches its max number or children."""
ui_message = "Max Children Reached"
message = message or "Max number of children reached"
super().__init__(ui_message, message=message, origin=origin)
msg = message or "Max number of children reached"
super().__init__(ui_message, message=msg, origin=origin)


class DDOSGuardError(CDLBaseError):
def __init__(self, message: str | None = None, *, origin: ScrapeItem | MediaItem | URL | None = None) -> None:
"""This error will be thrown when DDoS-Guard is detected."""
ui_message = "DDoS-Guard"
message = message or "DDoS-Guard detected"
super().__init__(ui_message, message=message, origin=origin)
msg = message or "DDoS-Guard detected"
super().__init__(ui_message, message=msg, origin=origin)


class DownloadError(CDLBaseError):
Expand All @@ -82,12 +82,14 @@ def __init__(
) -> None:
"""This error will be thrown when a download fails."""
ui_message = str(status)
msg = message
if isinstance(status, int):
try:
ui_message = f"{status} {HTTPStatus(status).phrase}"
msg = HTTPStatus(status).phrase
ui_message = f"{status} {msg}"
except ValueError:
ui_message = f"{status} HTTP Error"
super().__init__(ui_message, message=message, status=status, origin=origin)
super().__init__(ui_message, message=msg, status=status, origin=origin)


class SlowDownloadError(DownloadError):
Expand Down Expand Up @@ -150,12 +152,14 @@ def __init__(
) -> None:
"""This error will be thrown when a scrape fails."""
ui_message = str(status)
msg = message
if isinstance(status, int):
try:
ui_message = f"{status} {HTTPStatus(status).phrase}"
msg = HTTPStatus(status).phrase
ui_message = f"{status} {msg}"
except ValueError:
ui_message = f"{status} HTTP Error"
super().__init__(ui_message, message=message, status=status, origin=origin)
super().__init__(ui_message, message=msg, status=status, origin=origin)


class LoginError(CDLBaseError):
Expand Down
11 changes: 9 additions & 2 deletions cyberdrop_dl/downloader/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ async def wrapper(self: Downloader, *args, **kwargs) -> None:
if e.status != 999:
media_item.current_attempt += 1

log_message = f"with status {e.status} and message: {e.message}"
full_message = str(e.status)
if e.message != full_message:
full_message = f"{e.status} - {e.message}"

log_message = f"with error: {full_message}"
log(f"{self.log_prefix} failed: {media_item.url} {log_message}", 40)
if media_item.current_attempt < max_attempts:
retry_msg = f"Retrying {self.log_prefix.lower()}: {media_item.url} , retry attempt: {media_item.current_attempt + 1}"
Expand Down Expand Up @@ -161,7 +165,10 @@ async def download(self, media_item: MediaItem) -> None:

except (DownloadError, ClientResponseError) as e:
ui_message = getattr(e, "ui_message", e.status)
log_message_short = log_message = f"{e.status} - {e.message}"
full_message = e.message
if e.message != ui_message:
full_message = f"{e.status} - {e.message}"
log_message_short = log_message = full_message
log(f"{self.log_prefix} failed: {media_item.url} with error: {log_message}", 40)
await self.manager.log_manager.write_download_error_log(media_item.url, log_message_short, origin)
self.manager.progress_manager.download_stats_progress.add_failure(ui_message)
Expand Down
2 changes: 2 additions & 0 deletions cyberdrop_dl/managers/real_debrid/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ def handle_response(response: Response) -> dict | str | None:
response.raise_for_status()
JSONResp: dict = response.json()
except RequestException:
if response.status_code not in ERROR_CODES:
raise
raise RealDebridError(response, ERROR_CODES) from None
except AttributeError:
return response.text
Expand Down
3 changes: 2 additions & 1 deletion cyberdrop_dl/utils/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ async def wrapper(self: Crawler | Downloader, *args, **kwargs):
except Exception as e:
exc_info = e
if hasattr(e, "status") and hasattr(e, "message"):
log_message_short = log_message = e_ui_failure = f"{e.status} - {e.message}"
msg = f"{e.status} - {e.message}" if str(e.status) != e.message else e.message
log_message_short = log_message = e_ui_failure = msg
else:
log_message = str(e)
log_message_short = "See Log for Details"
Expand Down

0 comments on commit 60fadda

Please sign in to comment.