S-12 错误模型统一
这个提交存在于:
父节点
eb3ad2b630
当前提交
55850dc45b
共有 5 个文件被更改,包括 59 次插入 和 24 次删除
8
main.py
8
main.py
|
|
@ -10,6 +10,7 @@ from astrbot.api.star import Context, Star, StarTools
|
|||
|
||||
from .services.archive import ArchiveResult, ArchiveService
|
||||
from .services.download import DownloadProgress, JMDownloadService
|
||||
from .services.errors import JMDownloadError
|
||||
from .services.komga import KomgaClient, KomgaScanResult
|
||||
from .services.settings import PluginSettings
|
||||
|
||||
|
|
@ -69,9 +70,14 @@ class JMDownloadPlugin(Star):
|
|||
scan_result = await self.komga.scan_library()
|
||||
if self.settings.send_archive_file:
|
||||
file_send_message = await self._send_archive_file(event, archive_result)
|
||||
except JMDownloadError as exc:
|
||||
logger.warning(f"JMDownload expected failure for {comic_id}: {exc}")
|
||||
yield event.plain_result(f"JMDownload 失败:{exc.user_message}")
|
||||
event.stop_event()
|
||||
return
|
||||
except Exception as exc:
|
||||
logger.exception(f"JMDownload failed for {comic_id}")
|
||||
yield event.plain_result(f"JMDownload 失败:{exc}")
|
||||
yield event.plain_result("JMDownload 发生未预期错误,请查看 AstrBot 日志。")
|
||||
event.stop_event()
|
||||
return
|
||||
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ from xml.sax.saxutils import escape
|
|||
from PIL import Image
|
||||
|
||||
from .download import DownloadedComic
|
||||
from .errors import ArchiveError
|
||||
from .images import safe_filename
|
||||
from .settings import PluginSettings
|
||||
|
||||
|
|
@ -27,10 +28,6 @@ class ArchiveResult:
|
|||
page_count: int
|
||||
|
||||
|
||||
class ArchiveError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ArchiveService:
|
||||
def __init__(self, settings: PluginSettings) -> None:
|
||||
self.settings = settings
|
||||
|
|
@ -96,7 +93,7 @@ class ArchiveService:
|
|||
|
||||
def _create_pdf(self, target_path: Path, downloaded: DownloadedComic) -> None:
|
||||
if not downloaded.image_files:
|
||||
raise ArchiveError("no images available for PDF creation")
|
||||
raise ArchiveError("没有可用于生成 PDF 的图片")
|
||||
|
||||
with tempfile.TemporaryDirectory(
|
||||
prefix=".jm_pdf_",
|
||||
|
|
@ -126,7 +123,7 @@ class ArchiveService:
|
|||
for image_path in image_files:
|
||||
images.append(self._open_pdf_image(image_path))
|
||||
if not images:
|
||||
raise ArchiveError("no valid images available for PDF chunk")
|
||||
raise ArchiveError("当前 PDF 分块没有可用图片")
|
||||
images[0].save(
|
||||
target_path,
|
||||
"PDF",
|
||||
|
|
@ -147,12 +144,12 @@ class ArchiveService:
|
|||
|
||||
def _merge_pdf_chunks(self, temp_pdfs: list[Path], target_path: Path) -> None:
|
||||
if not temp_pdfs:
|
||||
raise ArchiveError("no PDF chunks were generated")
|
||||
raise ArchiveError("没有生成任何 PDF 分块")
|
||||
|
||||
try:
|
||||
from PyPDF2 import PdfMerger
|
||||
except ImportError as exc:
|
||||
raise ArchiveError("PyPDF2 is not installed; check plugin requirements.txt") from exc
|
||||
raise ArchiveError("缺少 PyPDF2 依赖,请检查插件 requirements.txt") from exc
|
||||
|
||||
merger = PdfMerger()
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import yaml
|
|||
|
||||
from astrbot.api import logger
|
||||
|
||||
from .errors import DownloadError
|
||||
from .images import collect_image_files, infer_album_title
|
||||
from .settings import PluginSettings
|
||||
|
||||
|
|
@ -40,10 +41,6 @@ class DownloadProgress:
|
|||
ProgressCallback = Callable[[DownloadProgress], Awaitable[None]]
|
||||
|
||||
|
||||
class DownloadError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class JMDownloadService:
|
||||
def __init__(self, settings: PluginSettings) -> None:
|
||||
self.settings = settings
|
||||
|
|
@ -79,7 +76,7 @@ class JMDownloadService:
|
|||
)
|
||||
image_files = collect_image_files(download_dir)
|
||||
if not image_files:
|
||||
raise DownloadError("download finished but no image files were found")
|
||||
raise DownloadError("下载完成但没有找到图片文件")
|
||||
|
||||
await self._emit_progress(
|
||||
progress_callback,
|
||||
|
|
@ -100,7 +97,7 @@ class JMDownloadService:
|
|||
)
|
||||
except (TimeoutError, asyncio.TimeoutError) as exc:
|
||||
last_error = (
|
||||
f"download timed out after {self.settings.download_timeout_seconds}s"
|
||||
f"下载超过 {self.settings.download_timeout_seconds} 秒仍未完成"
|
||||
)
|
||||
logger.warning(
|
||||
f"JMDownload: {comic_id} attempt {attempt} timed out; "
|
||||
|
|
@ -122,7 +119,7 @@ class JMDownloadService:
|
|||
except asyncio.CancelledError:
|
||||
pass
|
||||
|
||||
raise DownloadError(last_error or "download failed")
|
||||
raise DownloadError(last_error or "下载失败")
|
||||
|
||||
def cleanup(self, downloaded: DownloadedComic) -> None:
|
||||
if not self.settings.delete_work_dir_after_archive:
|
||||
|
|
@ -134,7 +131,7 @@ class JMDownloadService:
|
|||
try:
|
||||
import jmcomic
|
||||
except ImportError as exc:
|
||||
raise DownloadError("jmcomic is not installed; check plugin requirements.txt") from exc
|
||||
raise DownloadError("缺少 jmcomic 依赖,请检查插件 requirements.txt") from exc
|
||||
|
||||
logger.info(f"JMDownload: downloading album {comic_id} with {config_path}")
|
||||
option = jmcomic.create_option_by_file(str(config_path))
|
||||
|
|
|
|||
26
services/errors.py
普通文件
26
services/errors.py
普通文件
|
|
@ -0,0 +1,26 @@
|
|||
from __future__ import annotations
|
||||
|
||||
|
||||
class JMDownloadError(Exception):
|
||||
"""Expected plugin error with a user-facing message."""
|
||||
|
||||
default_message = "任务执行失败"
|
||||
|
||||
def __init__(self, message: str | None = None, *, details: str = "") -> None:
|
||||
self.user_message = message or self.default_message
|
||||
self.details = details
|
||||
super().__init__(
|
||||
self.user_message if not details else f"{self.user_message}: {details}",
|
||||
)
|
||||
|
||||
|
||||
class ConfigError(JMDownloadError):
|
||||
default_message = "插件配置错误"
|
||||
|
||||
|
||||
class DownloadError(JMDownloadError):
|
||||
default_message = "下载失败"
|
||||
|
||||
|
||||
class ArchiveError(JMDownloadError):
|
||||
default_message = "归档失败"
|
||||
|
|
@ -35,7 +35,7 @@ class KomgaSettings:
|
|||
api_key=str(config.get("api_key", "")).strip(),
|
||||
scan_timeout_seconds=max(
|
||||
5,
|
||||
int(config.get("scan_timeout_seconds", 30) or 30),
|
||||
_int_value(config.get("scan_timeout_seconds"), 30),
|
||||
),
|
||||
)
|
||||
|
||||
|
|
@ -90,7 +90,7 @@ class PluginSettings:
|
|||
work_root=data_dir / "work",
|
||||
archive_root=archive_root,
|
||||
archive_format=archive_format,
|
||||
pdf_chunk_size=max(1, int(config.get("pdf_chunk_size", 80) or 80)),
|
||||
pdf_chunk_size=max(1, _int_value(config.get("pdf_chunk_size"), 80)),
|
||||
admin_only=bool(config.get("admin_only", True)),
|
||||
send_archive_file=bool(config.get("send_archive_file", False)),
|
||||
overwrite_existing=bool(config.get("overwrite_existing", False)),
|
||||
|
|
@ -99,21 +99,21 @@ class PluginSettings:
|
|||
),
|
||||
download_timeout_seconds=max(
|
||||
60,
|
||||
int(config.get("download_timeout_seconds", 3600) or 3600),
|
||||
_int_value(config.get("download_timeout_seconds"), 3600),
|
||||
),
|
||||
download_retry_count=max(
|
||||
0,
|
||||
int(config.get("download_retry_count", 2) or 0),
|
||||
_int_value(config.get("download_retry_count"), 2),
|
||||
),
|
||||
progress_interval_seconds=max(
|
||||
0,
|
||||
int(config.get("progress_interval_seconds", 30) or 0),
|
||||
_int_value(config.get("progress_interval_seconds"), 30),
|
||||
),
|
||||
max_concurrent_tasks=max(
|
||||
1,
|
||||
int(config.get("max_concurrent_tasks", 1) or 1),
|
||||
_int_value(config.get("max_concurrent_tasks"), 1),
|
||||
),
|
||||
jm_batch_count=max(1, int(config.get("jm_batch_count", 20) or 20)),
|
||||
jm_batch_count=max(1, _int_value(config.get("jm_batch_count"), 20)),
|
||||
jm_domains=[str(domain).strip() for domain in jm_domains if str(domain).strip()],
|
||||
komga=KomgaSettings.from_config(komga_config),
|
||||
)
|
||||
|
|
@ -122,3 +122,12 @@ class PluginSettings:
|
|||
self.data_dir.mkdir(parents=True, exist_ok=True)
|
||||
self.work_root.mkdir(parents=True, exist_ok=True)
|
||||
self.archive_root.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
|
||||
def _int_value(value: Any, default: int) -> int:
|
||||
if value in (None, ""):
|
||||
return default
|
||||
try:
|
||||
return int(value)
|
||||
except (TypeError, ValueError):
|
||||
return default
|
||||
|
|
|
|||
正在加载…
添加表格
在新工单中引用