Skip to content

The only downloader you need. 下载器的集大成者。

refactor(error): 统一错误类型,删除 StepError

XiaoYouChR33b2b326b0e4
diff --git a/app/assets/i18n/gd3.en_US.ts b/app/assets/i18n/gd3.en_US.ts
index 77ef534b..6d4327f2 100644
--- a/app/assets/i18n/gd3.en_US.ts
+++ b/app/assets/i18n/gd3.en_US.ts
@@ -5314,6 +5314,26 @@ Data will be copied to new location and the app will then exit. Please manually
<source>未找到适配 {target} 的 N_m3u8DL-RE 安装包</source>
<translation>No N_m3u8DL-RE installer package found for {target}</translation>
</message>
+ <message>
+ <location filename="../../error_catalog.py" line="24" />
+ <source>无法获取版本信息</source>
+ <translation>Unable to retrieve version information</translation>
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="25" />
+ <source>校验失败</source>
+ <translation>Checksum verification failed</translation>
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="26" />
+ <source>当前平台无可用更新</source>
+ <translation>No update is available for the current platform</translation>
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="27" />
+ <source>DMG 中未找到 .app</source>
+ <translation>No .app bundle was found in the DMG</translation>
+ </message>
</context>
<context>
<name>TaskPage</name>
@@ -5585,29 +5605,6 @@ Data will be copied to new location and the app will then exit. Please manually
<translation type="vanished">Redownload failed. Please try again later</translation>
</message>
</context>
- <context>
- <name>UpdateErrors</name>
- <message>
- <location filename="../../error_catalog.py" line="24" />
- <source>无法获取版本信息</source>
- <translation>Unable to retrieve version information</translation>
- </message>
- <message>
- <location filename="../../error_catalog.py" line="25" />
- <source>校验失败</source>
- <translation>Checksum verification failed</translation>
- </message>
- <message>
- <location filename="../../error_catalog.py" line="26" />
- <source>当前平台无可用更新</source>
- <translation>No update is available for the current platform</translation>
- </message>
- <message>
- <location filename="../../error_catalog.py" line="27" />
- <source>DMG 中未找到 .app</source>
- <translation>No .app bundle was found in the DMG</translation>
- </message>
- </context>
<context>
<name>UrlEditCard</name>
<message>
diff --git a/app/assets/i18n/gd3.es_ES.ts b/app/assets/i18n/gd3.es_ES.ts
index bd335844..61f602b7 100644
--- a/app/assets/i18n/gd3.es_ES.ts
+++ b/app/assets/i18n/gd3.es_ES.ts
@@ -4279,6 +4279,26 @@ Data will be copied to new location and the program will then exit. Por favor ma
<source>未找到适配 {target} 的 N_m3u8DL-RE 安装包</source>
<translation type="unfinished" />
</message>
+ <message>
+ <location filename="../../error_catalog.py" line="24" />
+ <source>无法获取版本信息</source>
+ <translation type="unfinished">无法获取版本信息</translation>
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="25" />
+ <source>校验失败</source>
+ <translation type="unfinished">校验失败</translation>
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="26" />
+ <source>当前平台无可用更新</source>
+ <translation type="unfinished">当前平台无可用更新</translation>
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="27" />
+ <source>DMG 中未找到 .app</source>
+ <translation type="unfinished">DMG 中未找到 .app</translation>
+ </message>
</context>
<context>
<name>TaskPage</name>
@@ -4471,29 +4491,6 @@ Data will be copied to new location and the program will then exit. Por favor ma
<translation type="unfinished">封面</translation>
</message>
</context>
- <context>
- <name>UpdateErrors</name>
- <message>
- <location filename="../../error_catalog.py" line="24" />
- <source>无法获取版本信息</source>
- <translation type="unfinished">无法获取版本信息</translation>
- </message>
- <message>
- <location filename="../../error_catalog.py" line="25" />
- <source>校验失败</source>
- <translation type="unfinished">校验失败</translation>
- </message>
- <message>
- <location filename="../../error_catalog.py" line="26" />
- <source>当前平台无可用更新</source>
- <translation type="unfinished">当前平台无可用更新</translation>
- </message>
- <message>
- <location filename="../../error_catalog.py" line="27" />
- <source>DMG 中未找到 .app</source>
- <translation type="unfinished">DMG 中未找到 .app</translation>
- </message>
- </context>
<context>
<name>UrlEditCard</name>
<message>
diff --git a/app/assets/i18n/gd3.ja_JP.ts b/app/assets/i18n/gd3.ja_JP.ts
index b2b47067..f96bbe77 100644
--- a/app/assets/i18n/gd3.ja_JP.ts
+++ b/app/assets/i18n/gd3.ja_JP.ts
@@ -5371,6 +5371,26 @@ FFmpeg: {2}</translation>
<source>未找到适配 {target} 的 N_m3u8DL-RE 安装包</source>
<translation>{target} に適合する N_m3u8DL-RE のインストールパッケージが見つかりません</translation>
</message>
+ <message>
+ <location filename="../../error_catalog.py" line="24" />
+ <source>无法获取版本信息</source>
+ <translation>バージョン情報を取得できません</translation>
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="25" />
+ <source>校验失败</source>
+ <translation>検証に失敗しました</translation>
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="26" />
+ <source>当前平台无可用更新</source>
+ <translation>現在のプラットフォームに利用可能な更新はありません</translation>
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="27" />
+ <source>DMG 中未找到 .app</source>
+ <translation>DMG 内に .app が見つかりません</translation>
+ </message>
</context>
<context>
<name>TaskPage</name>
@@ -5650,29 +5670,6 @@ FFmpeg: {2}</translation>
<translation type="vanished">再ダウンロードに失敗しました。しばらくしてから再試行してください</translation>
</message>
</context>
- <context>
- <name>UpdateErrors</name>
- <message>
- <location filename="../../error_catalog.py" line="24" />
- <source>无法获取版本信息</source>
- <translation>バージョン情報を取得できません</translation>
- </message>
- <message>
- <location filename="../../error_catalog.py" line="25" />
- <source>校验失败</source>
- <translation>検証に失敗しました</translation>
- </message>
- <message>
- <location filename="../../error_catalog.py" line="26" />
- <source>当前平台无可用更新</source>
- <translation>現在のプラットフォームに利用可能な更新はありません</translation>
- </message>
- <message>
- <location filename="../../error_catalog.py" line="27" />
- <source>DMG 中未找到 .app</source>
- <translation>DMG 内に .app が見つかりません</translation>
- </message>
- </context>
<context>
<name>UrlEditCard</name>
<message>
diff --git a/app/assets/i18n/gd3.pt_BR.ts b/app/assets/i18n/gd3.pt_BR.ts
index 4421b38a..8cf56274 100644
--- a/app/assets/i18n/gd3.pt_BR.ts
+++ b/app/assets/i18n/gd3.pt_BR.ts
@@ -4567,6 +4567,26 @@ Os dados serão copiados para o novo local e o programa será encerrado. Reabra
<source>未找到适配 {target} 的 N_m3u8DL-RE 安装包</source>
<translation>Não foi encontrado um pacote de instalação do N_m3u8DL-RE adequado para {target}</translation>
</message>
+ <message>
+ <location filename="../../error_catalog.py" line="24" />
+ <source>无法获取版本信息</source>
+ <translation>Não foi possível obter informações da versão</translation>
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="25" />
+ <source>校验失败</source>
+ <translation>Falha na verificação</translation>
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="26" />
+ <source>当前平台无可用更新</source>
+ <translation>Nenhuma atualização disponível para a plataforma atual</translation>
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="27" />
+ <source>DMG 中未找到 .app</source>
+ <translation>Não foi encontrado .app no DMG</translation>
+ </message>
</context>
<context>
<name>TaskPage</name>
@@ -4826,29 +4846,6 @@ Os dados serão copiados para o novo local e o programa será encerrado. Reabra
<translation type="vanished">Copiar valor de verificação</translation>
</message>
</context>
- <context>
- <name>UpdateErrors</name>
- <message>
- <location filename="../../error_catalog.py" line="24" />
- <source>无法获取版本信息</source>
- <translation>Não foi possível obter informações da versão</translation>
- </message>
- <message>
- <location filename="../../error_catalog.py" line="25" />
- <source>校验失败</source>
- <translation>Falha na verificação</translation>
- </message>
- <message>
- <location filename="../../error_catalog.py" line="26" />
- <source>当前平台无可用更新</source>
- <translation>Nenhuma atualização disponível para a plataforma atual</translation>
- </message>
- <message>
- <location filename="../../error_catalog.py" line="27" />
- <source>DMG 中未找到 .app</source>
- <translation>Não foi encontrado .app no DMG</translation>
- </message>
- </context>
<context>
<name>UrlEditCard</name>
<message>
diff --git a/app/assets/i18n/gd3.ru_RU.ts b/app/assets/i18n/gd3.ru_RU.ts
index 98d0923e..b9d3a25d 100644
--- a/app/assets/i18n/gd3.ru_RU.ts
+++ b/app/assets/i18n/gd3.ru_RU.ts
@@ -5371,6 +5371,26 @@ FFmpeg: {2}</translation>
<source>未找到适配 {target} 的 N_m3u8DL-RE 安装包</source>
<translation>Не найден установочный пакет N_m3u8DL-RE, подходящий для {target}</translation>
</message>
+ <message>
+ <location filename="../../error_catalog.py" line="24" />
+ <source>无法获取版本信息</source>
+ <translation>Не удалось получить информацию о версии</translation>
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="25" />
+ <source>校验失败</source>
+ <translation>Ошибка проверки</translation>
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="26" />
+ <source>当前平台无可用更新</source>
+ <translation>Для текущей платформы нет доступных обновлений</translation>
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="27" />
+ <source>DMG 中未找到 .app</source>
+ <translation>В DMG не найден .app</translation>
+ </message>
</context>
<context>
<name>TaskPage</name>
@@ -5650,29 +5670,6 @@ FFmpeg: {2}</translation>
<translation type="vanished">Не удалось скачать заново, повторите попытку позже</translation>
</message>
</context>
- <context>
- <name>UpdateErrors</name>
- <message>
- <location filename="../../error_catalog.py" line="24" />
- <source>无法获取版本信息</source>
- <translation>Не удалось получить информацию о версии</translation>
- </message>
- <message>
- <location filename="../../error_catalog.py" line="25" />
- <source>校验失败</source>
- <translation>Ошибка проверки</translation>
- </message>
- <message>
- <location filename="../../error_catalog.py" line="26" />
- <source>当前平台无可用更新</source>
- <translation>Для текущей платформы нет доступных обновлений</translation>
- </message>
- <message>
- <location filename="../../error_catalog.py" line="27" />
- <source>DMG 中未找到 .app</source>
- <translation>В DMG не найден .app</translation>
- </message>
- </context>
<context>
<name>UrlEditCard</name>
<message>
diff --git a/app/assets/i18n/gd3.zh_CN.ts b/app/assets/i18n/gd3.zh_CN.ts
index 63e6c8b3..8af7f780 100644
--- a/app/assets/i18n/gd3.zh_CN.ts
+++ b/app/assets/i18n/gd3.zh_CN.ts
@@ -4198,6 +4198,26 @@ http://example.com/{mp4,mkv}/video</source>
<source>未找到适配 {target} 的 N_m3u8DL-RE 安装包</source>
<translation type="unfinished" />
</message>
+ <message>
+ <location filename="../../error_catalog.py" line="24" />
+ <source>无法获取版本信息</source>
+ <translation type="unfinished" />
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="25" />
+ <source>校验失败</source>
+ <translation type="unfinished" />
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="26" />
+ <source>当前平台无可用更新</source>
+ <translation type="unfinished" />
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="27" />
+ <source>DMG 中未找到 .app</source>
+ <translation type="unfinished" />
+ </message>
</context>
<context>
<name>TaskPage</name>
@@ -4390,29 +4410,6 @@ http://example.com/{mp4,mkv}/video</source>
<translation type="unfinished" />
</message>
</context>
- <context>
- <name>UpdateErrors</name>
- <message>
- <location filename="../../error_catalog.py" line="24" />
- <source>无法获取版本信息</source>
- <translation type="unfinished" />
- </message>
- <message>
- <location filename="../../error_catalog.py" line="25" />
- <source>校验失败</source>
- <translation type="unfinished" />
- </message>
- <message>
- <location filename="../../error_catalog.py" line="26" />
- <source>当前平台无可用更新</source>
- <translation type="unfinished" />
- </message>
- <message>
- <location filename="../../error_catalog.py" line="27" />
- <source>DMG 中未找到 .app</source>
- <translation type="unfinished" />
- </message>
- </context>
<context>
<name>UrlEditCard</name>
<message>
diff --git a/app/assets/i18n/gd3.zh_HK.ts b/app/assets/i18n/gd3.zh_HK.ts
index 87fb9885..69e423de 100644
--- a/app/assets/i18n/gd3.zh_HK.ts
+++ b/app/assets/i18n/gd3.zh_HK.ts
@@ -5371,6 +5371,26 @@ FFmpeg: {2}</translation>
<source>未找到适配 {target} 的 N_m3u8DL-RE 安装包</source>
<translation>搵唔到適配 {target} 嘅 N_m3u8DL-RE 安裝包</translation>
</message>
+ <message>
+ <location filename="../../error_catalog.py" line="24" />
+ <source>无法获取版本信息</source>
+ <translation>無法取得版本資訊</translation>
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="25" />
+ <source>校验失败</source>
+ <translation>校驗失敗</translation>
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="26" />
+ <source>当前平台无可用更新</source>
+ <translation>目前平台冇可用更新</translation>
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="27" />
+ <source>DMG 中未找到 .app</source>
+ <translation>DMG 中未搵到 .app</translation>
+ </message>
</context>
<context>
<name>TaskPage</name>
@@ -5650,29 +5670,6 @@ FFmpeg: {2}</translation>
<translation type="vanished">重新下載失敗,請稍後再試</translation>
</message>
</context>
- <context>
- <name>UpdateErrors</name>
- <message>
- <location filename="../../error_catalog.py" line="24" />
- <source>无法获取版本信息</source>
- <translation>無法取得版本資訊</translation>
- </message>
- <message>
- <location filename="../../error_catalog.py" line="25" />
- <source>校验失败</source>
- <translation>校驗失敗</translation>
- </message>
- <message>
- <location filename="../../error_catalog.py" line="26" />
- <source>当前平台无可用更新</source>
- <translation>目前平台冇可用更新</translation>
- </message>
- <message>
- <location filename="../../error_catalog.py" line="27" />
- <source>DMG 中未找到 .app</source>
- <translation>DMG 中未搵到 .app</translation>
- </message>
- </context>
<context>
<name>UrlEditCard</name>
<message>
diff --git a/app/assets/i18n/gd3.zh_TW.ts b/app/assets/i18n/gd3.zh_TW.ts
index e4f48246..605ce040 100644
--- a/app/assets/i18n/gd3.zh_TW.ts
+++ b/app/assets/i18n/gd3.zh_TW.ts
@@ -5371,6 +5371,26 @@ FFmpeg: {2}</translation>
<source>未找到适配 {target} 的 N_m3u8DL-RE 安装包</source>
<translation>找不到適用於 {target} 的 N_m3u8DL-RE 安裝套件</translation>
</message>
+ <message>
+ <location filename="../../error_catalog.py" line="24" />
+ <source>无法获取版本信息</source>
+ <translation>無法取得版本資訊</translation>
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="25" />
+ <source>校验失败</source>
+ <translation>校驗失敗</translation>
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="26" />
+ <source>当前平台无可用更新</source>
+ <translation>目前平台無可用更新</translation>
+ </message>
+ <message>
+ <location filename="../../error_catalog.py" line="27" />
+ <source>DMG 中未找到 .app</source>
+ <translation>DMG 中找不到 .app</translation>
+ </message>
</context>
<context>
<name>TaskPage</name>
@@ -5650,29 +5670,6 @@ FFmpeg: {2}</translation>
<translation type="vanished">重新下載失敗,請稍後再試</translation>
</message>
</context>
- <context>
- <name>UpdateErrors</name>
- <message>
- <location filename="../../error_catalog.py" line="24" />
- <source>无法获取版本信息</source>
- <translation>無法取得版本資訊</translation>
- </message>
- <message>
- <location filename="../../error_catalog.py" line="25" />
- <source>校验失败</source>
- <translation>校驗失敗</translation>
- </message>
- <message>
- <location filename="../../error_catalog.py" line="26" />
- <source>当前平台无可用更新</source>
- <translation>目前平台無可用更新</translation>
- </message>
- <message>
- <location filename="../../error_catalog.py" line="27" />
- <source>DMG 中未找到 .app</source>
- <translation>DMG 中找不到 .app</translation>
- </message>
- </context>
<context>
<name>UrlEditCard</name>
<message>
diff --git a/app/error_catalog.py b/app/error_catalog.py
index 02f02de9..918b839e 100644
--- a/app/error_catalog.py
+++ b/app/error_catalog.py
@@ -1,8 +1,8 @@
from PySide6.QtCore import QCoreApplication, QT_TRANSLATE_NOOP as N
-def toLocalizedError(error, context: str = "TaskErrors") -> str:
- text = QCoreApplication.translate(context, error.message) or error.message
+def toLocalizedError(error) -> str:
+ text = QCoreApplication.translate("TaskErrors", error.message) or error.message
return text.format_map(error.params) if error.params else text
@@ -22,8 +22,7 @@ N("TaskErrors", "无法下载 {name}/{tag}/{asset}")
N("TaskErrors", "无法获取 PyPI 包信息: {package}")
N("TaskErrors", "{name} 未安装,请在设置中安装")
-N("UpdateErrors", "无法获取版本信息")
-N("UpdateErrors", "校验失败")
-N("UpdateErrors", "当前平台无可用更新")
-N("UpdateErrors", "DMG 中未找到 .app")
-N("UpdateErrors", "发生了意外错误:{detail}")
+N("TaskErrors", "无法获取版本信息")
+N("TaskErrors", "校验失败")
+N("TaskErrors", "当前平台无可用更新")
+N("TaskErrors", "DMG 中未找到 .app")
diff --git a/app/models/task.py b/app/models/task.py
index 9e2a1a5e..b0bfc023 100644
--- a/app/models/task.py
+++ b/app/models/task.py
@@ -39,18 +39,6 @@ class TaskError(Exception):
return self.message.format_map(self.params) if self.params else self.message
-@dataclass(frozen=True)
-class StepError:
- message: str
- params: dict = field(default_factory=dict)
-
- def __str__(self) -> str:
- return self.message.format_map(self.params)
-
- def __bool__(self) -> bool:
- return bool(self.message)
-
-
def toTaskError(error) -> TaskError:
if isinstance(error, TaskError):
return error
@@ -118,7 +106,7 @@ class TaskStep:
progress: float = 0
receivedBytes: int = 0
speed: int = 0
- error: StepError | None = field(default=None, repr=False, init=False)
+ error: TaskError | None = field(default=None, repr=False, init=False)
def _bindTask(self, task: Task):
self._task = task
@@ -142,7 +130,7 @@ class TaskStep:
if hasattr(self, "_task"):
self._task.updateStatus()
- def setError(self, error: StepError) -> None:
+ def setError(self, error: TaskError) -> None:
self.error = error
self.status = TaskStatus.FAILED
self.speed = 0
@@ -239,7 +227,7 @@ class Task:
return True
@property
- def lastError(self) -> StepError | None:
+ def lastError(self) -> TaskError | None:
for step in reversed(self.steps):
if step.status == TaskStatus.FAILED and step.error:
return step.error
@@ -440,9 +428,8 @@ class Task:
logger.info("{} stopped", self.name)
raise
except Exception as e:
- error = toTaskError(e)
if currentStep is not None:
- currentStep.setError(StepError(error.message, error.params))
+ currentStep.setError(toTaskError(e))
logger.opt(exception=e).error("{} failed", self.name)
raise
diff --git a/app/view/components/progress_toast.py b/app/view/components/progress_toast.py
index e181dfa1..2f574ad8 100644
--- a/app/view/components/progress_toast.py
+++ b/app/view/components/progress_toast.py
@@ -109,7 +109,7 @@ class ProgressToast(InfoBar):
self.iconWidget.icon = InfoBarIcon.ERROR
self.title = self.tr("下载更新失败")
self.content = (
- toLocalizedError(info.error, context="UpdateErrors")
+ toLocalizedError(info.error)
if info.error is not None else self.tr("未知错误")
)
self._retryButton.show()
diff --git a/tests/test_task_card.py b/tests/test_task_card.py
index 75255b4b..5e5821a5 100644
--- a/tests/test_task_card.py
+++ b/tests/test_task_card.py
@@ -8,7 +8,7 @@ from dataclasses import dataclass
import pytest
-from app.models.task import Task, TaskStep, TaskStatus, StepError
+from app.models.task import Task, TaskStep, TaskStatus, TaskError
@dataclass(kw_only=True)
@@ -63,7 +63,7 @@ def makeCard(qapp, status, fileSize=1000, withError=False):
step.progress = 100
step.receivedBytes = fileSize
if withError:
- step.error = StepError("测试错误")
+ step.error = TaskError("测试错误")
task = Task(name="test.txt", url="http://test", packId="test",
steps=[step], fileSize=fileSize)
diff --git a/tests/test_task_status.py b/tests/test_task_status.py
index 68f7fdcf..ea3d441f 100644
--- a/tests/test_task_status.py
+++ b/tests/test_task_status.py
@@ -9,7 +9,7 @@ from dataclasses import dataclass
import pytest
-from app.models.task import Task, TaskStep, TaskStatus, StepError
+from app.models.task import Task, TaskStep, TaskStatus, TaskError
@dataclass(kw_only=True)
@@ -114,7 +114,7 @@ class TestSetStatus:
task = makeTask(TaskStatus.FAILED)
task.steps[0].progress = 50
task.steps[0].receivedBytes = 1024
- task.steps[0].error = StepError("test error")
+ task.steps[0].error = TaskError("test error")
task.setStatus(TaskStatus.RUNNING)
assert task.steps[0].progress == 0
assert task.steps[0].receivedBytes == 0