From 2734177d94862e2c0e06d985ab65091d5d002dea Mon Sep 17 00:00:00 2001 From: imSp4rky Date: Wed, 25 Nov 2026 16:45:51 -0700 Subject: [PATCH] fix(api): keep the error code a download failure names in the job error --- tests/remote/unit/test_download_manager.py | 45 ++++++++++++++++++++++ unshackle/core/api/download_manager.py | 4 +- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/tests/remote/unit/test_download_manager.py b/tests/remote/unit/test_download_manager.py index a0bf0f98..c1b6c0c9 100644 --- a/tests/remote/unit/test_download_manager.py +++ b/tests/remote/unit/test_download_manager.py @@ -711,3 +711,48 @@ def test_job_history_keeps_the_skipped_titles(tmp_path: Path, manager: DownloadQ record_job_history(job) entry = json.loads((tmp_path / "api_history.jsonl").read_text(encoding="utf-8").splitlines()[-1]) assert entry["skipped_titles"] == job.skipped_titles + + +@pytest.mark.parametrize("carrier", ["error", "cause", "none"]) +def test_a_download_error_keeps_the_code_its_raiser_named( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, carrier: str +) -> None: + import click + + import unshackle.commands.dl as dl_module + from unshackle.core.api import download_manager, handlers + from unshackle.core.api.errors import APIError + from unshackle.core.remote_protocol import APIErrorCode, coded + from unshackle.core.run_plan import RunOutcome + from unshackle.core.services import Services + + error = ValueError("no key for the track") + if carrier == "error": + coded(error, APIErrorCode.DRM_ERROR) + elif carrier == "cause": + error.__cause__ = coded(KeyError("kid"), APIErrorCode.DRM_ERROR) + + class FakeDl: + cli = dl_module.dl.cli + + def __init__(self, ctx: click.Context, **kwargs: object) -> None: + pass + + def result(self, **kwargs: object) -> RunOutcome: + return RunOutcome(error=error) + + def failure_lines(self, error: BaseException) -> tuple[str, str]: + return "", str(error) + + class FakeService: + cli = click.Command("svc", callback=lambda title: object()) + + monkeypatch.setattr(dl_module, "dl", FakeDl) + monkeypatch.setattr(Services, "get_path", staticmethod(lambda s: tmp_path)) + monkeypatch.setattr(Services, "load", staticmethod(lambda s: FakeService)) + monkeypatch.setattr(handlers, "load_full_cdm", lambda *a: None) + + with pytest.raises(APIError, match="^download worker failed: no key for the track$") as exc: + download_manager.perform_download("job-1", "EXAMPLE1", "t1", {}) + + assert exc.value.error_code == (APIErrorCode.WORKER_ERROR if carrier == "none" else APIErrorCode.DRM_ERROR) diff --git a/unshackle/core/api/download_manager.py b/unshackle/core/api/download_manager.py index 086ad0b5..a2e89acf 100644 --- a/unshackle/core/api/download_manager.py +++ b/unshackle/core/api/download_manager.py @@ -474,7 +474,9 @@ def perform_download( if outcome.error is not None: # segment URLs carry signed CDN tokens (hmac, session IDs) that redact_text does not match detail = redact_all(dl_instance.failure_lines(outcome.error)[1].strip()) - raise APIError(APIErrorCode.WORKER_ERROR, f"download worker failed: {detail}") + named = (getattr(e, "error_code", None) for e in (outcome.error, outcome.error.__cause__)) + code = next((c for c in named if isinstance(c, APIErrorCode)), APIErrorCode.WORKER_ERROR) + raise APIError(code, f"download worker failed: {detail}") if progress_callback and outcome.skipped_subtitles: progress_callback({"skipped_subtitles": outcome.skipped_subtitles})