Skip to content

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

feat(m3u8): 分类 N_m3u8DL-RE 错误信息,替代原始 stack trace

XiaoYouChR192400f3e096
diff --git a/features/m3u8_pack/error_catalog.py b/features/m3u8_pack/error_catalog.py
new file mode 100644
index 00000000..1f11bdeb
--- /dev/null
+++ b/features/m3u8_pack/error_catalog.py
@@ -0,0 +1,9 @@
+from PySide6.QtCore import QT_TRANSLATE_NOOP as N
+
+N("TaskErrors", "服务器拒绝了请求(403),链接可能已失效({detail})")
+N("TaskErrors", "资源不存在(404),链接可能已失效({detail})")
+N("TaskErrors", "服务器返回了错误({detail})")
+N("TaskErrors", "无法解析域名,请检查网络连接({detail})")
+N("TaskErrors", "连接被拒绝,请检查网络连接({detail})")
+N("TaskErrors", "连接超时,请检查网络连接({detail})")
+N("TaskErrors", "SSL 连接失败,请检查网络连接({detail})")
diff --git a/features/m3u8_pack/task.py b/features/m3u8_pack/task.py
index e4830977..920f5643 100644
--- a/features/m3u8_pack/task.py
+++ b/features/m3u8_pack/task.py
@@ -36,6 +36,49 @@ DECRYPTION_ENGINES = {
"Shaka Packager": "SHAKA_PACKAGER",
}
+ERROR_HINTS = (
+ ("response status code does not indicate success: 403",
+ "服务器拒绝了请求(403),链接可能已失效({detail})"),
+ ("response status code does not indicate success: 404",
+ "资源不存在(404),链接可能已失效({detail})"),
+ ("response status code does not indicate success:",
+ "服务器返回了错误({detail})"),
+ ("no such host is known",
+ "无法解析域名,请检查网络连接({detail})"),
+ ("connection refused",
+ "连接被拒绝,请检查网络连接({detail})"),
+ ("the request was canceled due to the configured httpclient.timeout",
+ "连接超时,请检查网络连接({detail})"),
+ ("the ssl connection could not be established",
+ "SSL 连接失败,请检查网络连接({detail})"),
+)
+
+LOG_LINE_PATTERN = re.compile(
+ r"\d{2}:\d{2}:\d{2}\.\d{3}\s+(WARN|ERROR)\s*:\s*(.*)"
+)
+
+
+def parseError(output: str, returncode: int) -> TaskError:
+ lowered = output.lower()
+ for needle, message in ERROR_HINTS:
+ if needle in lowered:
+ detail = parseDiagnostic(output)
+ return TaskError(message, detail=detail)
+
+ detail = parseDiagnostic(output)
+ return TaskError(
+ "进程异常退出({code}):{detail}",
+ code=returncode,
+ detail=detail or "N_m3u8DL-RE",
+ )
+
+
+def parseDiagnostic(output: str) -> str:
+ last = ""
+ for m in LOG_LINE_PATTERN.finditer(output):
+ last = m.group(2).strip()
+ return last
+
@dataclass(kw_only=True, eq=False)
class M3U8Task(Task):
@@ -422,11 +465,7 @@ class M3U8TaskStep(TaskStep):
output = getattr(self, "_processOutput", "")
if output:
logger.warning("N_m3u8DL-RE output:\n{}", output[-8192:])
- raise TaskError(
- "进程异常退出({code}):{detail}",
- code=self._process.returncode,
- detail=self.lastMessage or "N_m3u8DL-RE",
- )
+ raise parseError(output, self._process.returncode)
if not self._findOutputFile():
raise TaskError(
diff --git a/tests/test_m3u8_error.py b/tests/test_m3u8_error.py
new file mode 100644
index 00000000..1dde9fa7
--- /dev/null
+++ b/tests/test_m3u8_error.py
@@ -0,0 +1,145 @@
+"""parseError / parseDiagnostic 的错误解析测试。
+
+Seam: parseError — 输入 N_m3u8DL-RE 完整 stdout 和 exit code,
+输出 TaskError(分类消息 + detail)。
+"""
+from features.m3u8_pack.task import parseDiagnostic, parseError
+
+
+# -- parseDiagnostic ----------------------------------------------------------
+
+class TestParseDiagnostic:
+
+ def test_extracts_last_warn_line(self):
+ output = (
+ "22:17:47.595 INFO : N_m3u8DL-RE (Beta version) 20260628\n"
+ "22:17:48.730 WARN : Response status code does not indicate success: 403 (Forbidden). (1/10)\n"
+ "22:17:50.235 INFO : Loading URL: http://example.com\n"
+ "22:17:51.306 WARN : Response status code does not indicate success: 403 (Forbidden). (2/10)\n"
+ )
+ assert parseDiagnostic(output) == (
+ "Response status code does not indicate success: 403 (Forbidden). (2/10)"
+ )
+
+ def test_prefers_error_over_warn(self):
+ output = (
+ "22:17:48.730 WARN : some warning\n"
+ "22:18:00.000 ERROR: ffmpeg not found\n"
+ )
+ assert parseDiagnostic(output) == "ffmpeg not found"
+
+ def test_ignores_info_lines(self):
+ output = "22:17:47.595 INFO : N_m3u8DL-RE started\n"
+ assert parseDiagnostic(output) == ""
+
+ def test_ignores_stack_trace_lines(self):
+ output = (
+ "22:18:14.893 WARN : 403 (Forbidden). (10/10)\n"
+ "Unhandled exception: System.Exception: Failed\n"
+ " ---> System.Net.Http.HttpRequestException: 403\n"
+ " at System.Net.Http.EnsureSuccessStatusCode()\n"
+ )
+ assert parseDiagnostic(output) == "403 (Forbidden). (10/10)"
+
+ def test_concatenated_warn_and_unhandled_exception(self):
+ output = (
+ "22:18:14.893 WARN : 403 (Forbidden). (10/10)"
+ "Unhandled exception: System.Exception: Failed\n"
+ " ---> System.Net.Http.HttpRequestException: 403\n"
+ )
+ result = parseDiagnostic(output)
+ assert "403 (Forbidden). (10/10)" in result
+
+ def test_empty_output(self):
+ assert parseDiagnostic("") == ""
+
+ def test_error_colon_no_space(self):
+ output = "22:18:00.000 ERROR: Failed\n"
+ assert parseDiagnostic(output) == "Failed"
+
+
+# -- parseError ---------------------------------------------------------------
+
+class TestParseError:
+
+ def test_matches_403(self):
+ output = (
+ "22:18:14.893 WARN : Response status code does not indicate success: "
+ "403 (Forbidden). (10/10)\n"
+ )
+ error = parseError(output, 1)
+ assert "403" in str(error)
+ assert "服务器拒绝了请求" in error.message
+
+ def test_matches_404(self):
+ output = (
+ "22:18:14.893 WARN : Response status code does not indicate success: "
+ "404 (Not Found). (3/10)\n"
+ )
+ error = parseError(output, 1)
+ assert "资源不存在" in error.message
+
+ def test_matches_generic_http_error(self):
+ output = (
+ "22:18:14.893 WARN : Response status code does not indicate success: "
+ "500 (Internal Server Error). (1/10)\n"
+ )
+ error = parseError(output, 1)
+ assert "服务器返回了错误" in error.message
+
+ def test_matches_dns_failure(self):
+ output = "22:18:00.000 WARN : No such host is known. (1/10)\n"
+ error = parseError(output, 1)
+ assert "无法解析域名" in error.message
+
+ def test_matches_connection_refused(self):
+ output = "22:18:00.000 WARN : Connection refused (1/10)\n"
+ error = parseError(output, 1)
+ assert "连接被拒绝" in error.message
+
+ def test_matches_timeout(self):
+ output = (
+ "22:18:00.000 WARN : The request was canceled due to the configured "
+ "HttpClient.Timeout (1/10)\n"
+ )
+ error = parseError(output, 1)
+ assert "连接超时" in error.message
+
+ def test_matches_ssl_error(self):
+ output = (
+ "22:18:00.000 WARN : The SSL connection could not be established (1/10)\n"
+ )
+ error = parseError(output, 1)
+ assert "SSL 连接失败" in error.message
+
+ def test_fallback_with_diagnostic(self):
+ output = "22:18:00.000 ERROR: some unknown error\n"
+ error = parseError(output, 1)
+ assert "进程异常退出" in error.message
+ assert error.params["detail"] == "some unknown error"
+ assert error.params["code"] == 1
+
+ def test_fallback_empty_output(self):
+ error = parseError("", 1)
+ assert "进程异常退出" in error.message
+ assert error.params["detail"] == "N_m3u8DL-RE"
+
+ def test_403_specific_beats_generic_http(self):
+ output = (
+ "22:18:14.893 WARN : Response status code does not indicate success: "
+ "403 (Forbidden). (10/10)\n"
+ )
+ error = parseError(output, 1)
+ assert "服务器拒绝了请求" in error.message
+ assert "服务器返回了错误" not in error.message
+
+ def test_detail_contains_diagnostic(self):
+ output = (
+ "22:18:14.893 WARN : Response status code does not indicate success: "
+ "403 (Forbidden). (10/10)\n"
+ "Unhandled exception: System.Exception: Failed\n"
+ " ---> System.Net.Http.HttpRequestException: 403\n"
+ " at System.Net.Http.EnsureSuccessStatusCode()\n"
+ )
+ error = parseError(output, 1)
+ assert "403 (Forbidden). (10/10)" in error.params["detail"]