Skip to content

Bulk asynchronous downloader for multiple file hosts

fix: prettify errors

NTFSvolume314ed445dfa1
diff --git a/cyberdrop_dl/clients/errors.py b/cyberdrop_dl/clients/errors.py
index 863b0f102..566bce8c9 100644
--- a/cyberdrop_dl/clients/errors.py
+++ b/cyberdrop_dl/clients/errors.py
@@ -1,5 +1,7 @@
from __future__ import annotations
+import contextlib
+from functools import singledispatch
from http import HTTPStatus
from pathlib import Path
from typing import TYPE_CHECKING
@@ -81,14 +83,8 @@ class DownloadError(CDLBaseError):
self, status: str | int, message: str | None = None, origin: ScrapeItem | MediaItem | URL | None = None
) -> None:
"""This error will be thrown when a download fails."""
- ui_message = str(status)
+ ui_message = create_error_msg(status)
msg = message
- if isinstance(status, int):
- try:
- msg = HTTPStatus(status).phrase
- ui_message = f"{status} {msg}"
- except ValueError:
- ui_message = f"{status} HTTP Error"
super().__init__(ui_message, message=msg, status=status, origin=origin)
@@ -138,14 +134,8 @@ class ScrapeError(CDLBaseError):
self, status: str | int, message: str | None = None, origin: ScrapeItem | MediaItem | URL | None = None
) -> None:
"""This error will be thrown when a scrape fails."""
- ui_message = str(status)
+ ui_message = create_error_msg(status)
msg = message
- if isinstance(status, int):
- try:
- msg = HTTPStatus(status).phrase
- ui_message = f"{status} {msg}"
- except ValueError:
- ui_message = f"{status} HTTP Error"
super().__init__(ui_message, message=msg, status=status, origin=origin)
@@ -166,3 +156,16 @@ class InvalidYamlError(CDLBaseError):
mark = e.problem_mark if hasattr(e, "problem_mark") else e
message = f"File '{file.resolve()}' has an invalid config. Please verify and edit it manually\n {mark}\n\n{VALIDATION_ERROR_FOOTER}"
super().__init__("Invalid YAML", message=message, origin=file)
+
+
+@singledispatch
+def create_error_msg(error: int) -> str:
+ with contextlib.suppress(ValueError):
+ msg = HTTPStatus(error).phrase
+ return f"{error} {msg}"
+ return f"{error} HTTP Error"
+
+
+@create_error_msg.register
+def _(error: str) -> str:
+ return error
diff --git a/cyberdrop_dl/downloader/downloader.py b/cyberdrop_dl/downloader/downloader.py
index aaaa2621b..62ca2cfa9 100644
--- a/cyberdrop_dl/downloader/downloader.py
+++ b/cyberdrop_dl/downloader/downloader.py
@@ -11,7 +11,12 @@ from typing import TYPE_CHECKING
from aiohttp import ClientError, ClientResponseError
from filedate import File
-from cyberdrop_dl.clients.errors import DownloadError, InsufficientFreeSpaceError, RestrictedFiletypeError
+from cyberdrop_dl.clients.errors import (
+ DownloadError,
+ InsufficientFreeSpaceError,
+ RestrictedFiletypeError,
+ create_error_msg,
+)
from cyberdrop_dl.utils.constants import CustomHTTPStatus
from cyberdrop_dl.utils.logger import log
from cyberdrop_dl.utils.utilities import error_handling_wrapper
@@ -24,7 +29,7 @@ if TYPE_CHECKING:
from cyberdrop_dl.utils.data_enums_classes.url_objects import MediaItem
-def retry(func: Callable) -> None:
+def retry(func: Callable) -> Callable:
"""This function is a wrapper that handles retrying for failed downloads."""
@wraps(func)
@@ -165,7 +170,7 @@ class Downloader:
self.attempt_task_removal(media_item)
except (DownloadError, ClientResponseError) as e:
- ui_message = getattr(e, "ui_message", e.status)
+ ui_message = create_error_msg(getattr(e, "ui_message", e.status))
full_message = e.message
if e.message != ui_message:
full_message = f"{e.status} - {e.message}"
diff --git a/cyberdrop_dl/ui/progress/statistic_progress.py b/cyberdrop_dl/ui/progress/statistic_progress.py
index bf6451a3c..209fe8357 100644
--- a/cyberdrop_dl/ui/progress/statistic_progress.py
+++ b/cyberdrop_dl/ui/progress/statistic_progress.py
@@ -1,6 +1,6 @@
from __future__ import annotations
-import re
+import contextlib
from functools import lru_cache
from typing import NamedTuple
@@ -8,7 +8,12 @@ from rich.console import Group
from rich.panel import Panel
from rich.progress import BarColumn, Progress, TaskID
-SPLIT_BY_UPPERCASE_REGEX = re.compile(r"[A-Z][a-z]*|[a-z]+|\d+")
+FAILURE_OVERRIDES = {
+ "ClientConnectorCertificateError": "Client Connector Certificate Error",
+ "ClientConnectorDNSError": "Client Connector DNS Error",
+ "ClientConnectorError": "Client Connector Error",
+ "ConnectionTimeoutError": "Connection Timeout Error",
+}
class TaskInfo(NamedTuple):
@@ -106,7 +111,7 @@ class StatsProgress:
def add_failure(self, failure: str) -> None:
"""Adds a failed file to the progress bar."""
self.failed_files += 1
- key = prettify_failure(failure)
+ key = get_pretty_failure(failure)
task_id = self.failure_types.get(key)
if task_id is not None:
self.progress.advance(task_id)
@@ -148,9 +153,18 @@ class ScrapeStatsProgress(StatsProgress):
@lru_cache
-def prettify_failure(failure: str) -> str:
- return " ".join(s.capitalize() for s in split_by_uppercase(failure))
+def get_pretty_failure(failure: str) -> str:
+ with contextlib.suppress(KeyError):
+ return FAILURE_OVERRIDES[failure]
+ return capitalize_words(failure)
-def split_by_uppercase(text: str) -> list[str]:
- return re.findall(SPLIT_BY_UPPERCASE_REGEX, text)
+def capitalize_words(text: str) -> str:
+ """Capitalize first letter of each word
+
+ Unlike `str.capwords()`, this only caps the first letter of each word without modifying the rest of the word"""
+ return " ".join([capitalize_first_letter(word) for word in text.split()])
+
+
+def capitalize_first_letter(word: str) -> str:
+ return word[0].capitalize() + word[1:]