mirror of
https://github.com/beetbox/beets.git
synced 2026-02-08 16:34:12 +01:00
Gracefully handle 404s when importing from MusicBrainz. (#6292)
A 404 error can be raised when fetching from MusicBrainz in the case of re-importing an album that has since been deleted from MusicBrainz.
This commit is contained in:
commit
4572a7dc8a
2 changed files with 38 additions and 1 deletions
|
|
@ -792,7 +792,13 @@ class MusicBrainzPlugin(MusicBrainzAPIMixin, MetadataSourcePlugin):
|
|||
self._log.debug("Invalid MBID ({}).", album_id)
|
||||
return None
|
||||
|
||||
res = self.mb_api.get_release(albumid, includes=RELEASE_INCLUDES)
|
||||
# A 404 error here is fine. e.g. re-importing a release that has
|
||||
# been deleted on MusicBrainz.
|
||||
try:
|
||||
res = self.mb_api.get_release(albumid, includes=RELEASE_INCLUDES)
|
||||
except HTTPNotFoundError:
|
||||
self._log.debug("Release {} not found on MusicBrainz.", albumid)
|
||||
return None
|
||||
|
||||
# resolve linked release relations
|
||||
actual_res = None
|
||||
|
|
|
|||
|
|
@ -15,10 +15,12 @@
|
|||
"""Tests for MusicBrainz API wrapper."""
|
||||
|
||||
import unittest
|
||||
import uuid
|
||||
from typing import ClassVar
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
import requests
|
||||
|
||||
from beets import config
|
||||
from beets.library import Item
|
||||
|
|
@ -1106,3 +1108,32 @@ class TestMusicBrainzPlugin(PluginMixin):
|
|||
assert len(candidates) == 1
|
||||
assert candidates[0].tracks[0].track_id == self.RECORDING["id"]
|
||||
assert candidates[0].album == "hi"
|
||||
|
||||
def test_import_handles_404_gracefully(self, mb, requests_mock):
|
||||
id_ = uuid.uuid4()
|
||||
response = requests.Response()
|
||||
response.status_code = 404
|
||||
requests_mock.get(
|
||||
f"/ws/2/release/{id_}",
|
||||
exc=requests.exceptions.HTTPError(response=response),
|
||||
)
|
||||
res = mb.album_for_id(str(id_))
|
||||
assert res is None
|
||||
|
||||
def test_import_propagates_non_404_errors(self, mb):
|
||||
class DummyResponse:
|
||||
status_code = 500
|
||||
|
||||
error = requests.exceptions.HTTPError(response=DummyResponse())
|
||||
|
||||
def raise_error(*args, **kwargs):
|
||||
raise error
|
||||
|
||||
# Simulate mb.mb_api.get_release raising a non-404 HTTP error
|
||||
mb.mb_api.get_release = raise_error
|
||||
|
||||
with pytest.raises(requests.exceptions.HTTPError) as excinfo:
|
||||
mb.album_for_id(str(uuid.uuid4()))
|
||||
|
||||
# Ensure the exact error is propagated, not swallowed
|
||||
assert excinfo.value is error
|
||||
|
|
|
|||
Loading…
Reference in a new issue