Skip to content

Commit fc9d4d7

Browse files
Merge branch 'master' into badfiles_auto_action
2 parents 5699707 + 4572a7d commit fc9d4d7

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

beetsplug/musicbrainz.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,13 @@ def album_for_id(
792792
self._log.debug("Invalid MBID ({}).", album_id)
793793
return None
794794

795-
res = self.mb_api.get_release(albumid, includes=RELEASE_INCLUDES)
795+
# A 404 error here is fine. e.g. re-importing a release that has
796+
# been deleted on MusicBrainz.
797+
try:
798+
res = self.mb_api.get_release(albumid, includes=RELEASE_INCLUDES)
799+
except HTTPNotFoundError:
800+
self._log.debug("Release {} not found on MusicBrainz.", albumid)
801+
return None
796802

797803
# resolve linked release relations
798804
actual_res = None

test/plugins/test_musicbrainz.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
"""Tests for MusicBrainz API wrapper."""
1616

1717
import unittest
18+
import uuid
1819
from typing import ClassVar
1920
from unittest import mock
2021

2122
import pytest
23+
import requests
2224

2325
from beets import config
2426
from beets.library import Item
@@ -1106,3 +1108,32 @@ def test_candidates(self, monkeypatch, mb):
11061108
assert len(candidates) == 1
11071109
assert candidates[0].tracks[0].track_id == self.RECORDING["id"]
11081110
assert candidates[0].album == "hi"
1111+
1112+
def test_import_handles_404_gracefully(self, mb, requests_mock):
1113+
id_ = uuid.uuid4()
1114+
response = requests.Response()
1115+
response.status_code = 404
1116+
requests_mock.get(
1117+
f"/ws/2/release/{id_}",
1118+
exc=requests.exceptions.HTTPError(response=response),
1119+
)
1120+
res = mb.album_for_id(str(id_))
1121+
assert res is None
1122+
1123+
def test_import_propagates_non_404_errors(self, mb):
1124+
class DummyResponse:
1125+
status_code = 500
1126+
1127+
error = requests.exceptions.HTTPError(response=DummyResponse())
1128+
1129+
def raise_error(*args, **kwargs):
1130+
raise error
1131+
1132+
# Simulate mb.mb_api.get_release raising a non-404 HTTP error
1133+
mb.mb_api.get_release = raise_error
1134+
1135+
with pytest.raises(requests.exceptions.HTTPError) as excinfo:
1136+
mb.album_for_id(str(uuid.uuid4()))
1137+
1138+
# Ensure the exact error is propagated, not swallowed
1139+
assert excinfo.value is error

0 commit comments

Comments
 (0)