fix(worker): catch mutagen TypeError when saving metadata (#3182)

Mutagen may fail with:

```
worker-1  | Traceback (most recent call last):
worker-1  |   File "/usr/local/lib/python3.10/site-packages/celery/app/trace.py", line 412, in trace_task
worker-1  |     R = retval = fun(*args, **kwargs)
worker-1  |   File "/usr/local/lib/python3.10/site-packages/celery/app/trace.py", line 704, in __protected_call__
worker-1  |     return self.run(*args, **kwargs)
worker-1  |   File "/src/libretime_worker/tasks.py", line 114, in podcast_download
worker-1  |     metadata["artist"] = podcast_name
worker-1  |   File "/usr/local/lib/python3.10/site-packages/mutagen/_file.py", line 74, in __setitem__
worker-1  |     self.tags[key] = value
worker-1  |   File "/usr/local/lib/python3.10/site-packages/mutagen/id3/_tags.py", line 341, in __setitem__
worker-1  |     raise TypeError("%r not a Frame instance" % tag)
worker-1  | TypeError: "Infos du soir" not a Frame instance
```

This TypeError was raised when I trying to write metadata to a wave
file.

This patch ensures that the error is reported back to the frontend. It
also improve the error messages being reported.
This commit is contained in:
Jonas L. 2025-07-16 20:24:02 +02:00 committed by GitHub
parent 4b34070a72
commit 107bacf296
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 11 deletions

View File

@ -60,6 +60,12 @@ def legacy_trigger_task_manager():
legacy_client.trigger_task_manager()
class PodcastDownloadException(Exception):
"""
An error occurred during the podcast download task.
"""
@worker.task(name="podcast-download", acks_late=True)
def podcast_download(
episode_id: int,
@ -98,15 +104,16 @@ def podcast_download(
tmp_file.write(chunk)
except RequestException as exception:
logger.exception("could not download podcast episode %s", episode_id)
raise exception
raise PodcastDownloadException(
f"could not download podcast episode {episode_id}: {exception}"
) from exception
# Save metadata to podcast episode file
try:
metadata = mutagen.File(tmp_file.name, easy=True)
if metadata is None:
raise MutagenError(
f"could not determine episode {episode_id} file type"
raise PodcastDownloadException(
f"could not determine podcast episode {episode_id} file type"
)
if override_album:
@ -122,9 +129,10 @@ def podcast_download(
metadata.save()
logger.debug("saved metadata %s", metadata)
except MutagenError as exception:
logger.exception("could not save episode %s metadata", episode_id)
raise exception
except (MutagenError, TypeError) as exception:
raise PodcastDownloadException(
f"could not save podcast episode {episode_id} metadata: {exception}"
) from exception
# Upload podcast episode file
try:
@ -141,10 +149,12 @@ def podcast_download(
result["status"] = 1
except RequestException as exception:
logger.exception("could not upload episode %s", episode_id)
raise exception
raise PodcastDownloadException(
f"could not upload podcast episode {episode_id}: {exception}"
) from exception
except (RequestException, MutagenError) as exception:
except PodcastDownloadException as exception:
logger.exception(exception)
result["status"] = 0
result["error"] = str(exception)

View File

@ -53,7 +53,7 @@ def test_podcast_download_invalid_file(requests_mock):
assert json.loads(result) == {
"episodeid": 1,
"status": 0,
"error": "could not determine episode 1 file type",
"error": "could not determine podcast episode 1 file type",
}