Repository: beetbox/beets

Issue:
spotify: Crash when search encounters a 404 error
I sometimes run into this error when I import tracks as singletons and a 404 is encountered. My expectation was that the import will continue with the next track, but that is not happening and the import process just exits. This only happens when I accept Musicbrainz as a match. 

I just modified the API error to include more details and here is the trace:

```
/home/arsaboo/shared/music/Coke Studio @ MTV Season 2 (2012)/Coke Studio @ MTV Season 1 (2011) - Dheere Dheere.flac
Tagging track: Megha Sriram Dalton - Dheere Dheere
URL:
    https://musicbrainz.org/recording/efc2a2cd-9ed8-4436-8007-0ebe1a166873
(Similarity: 100.0%)
[A]pply, More candidates, Skip, Use as-is, Enter search, enter Id, aBort,
eDit, edit Candidates? a
Traceback (most recent call last):
  File "/home/arsaboo/.local/bin/beet", line 8, in <module>
    sys.exit(main())
  File "/home/arsaboo/.local/lib/python3.10/site-packages/beets/ui/__init__.py", line 1304, in main
    _raw_main(args)
  File "/home/arsaboo/.local/lib/python3.10/site-packages/beets/ui/__init__.py", line 1291, in _raw_main
    subcommand.func(lib, suboptions, subargs)
  File "/home/arsaboo/.local/lib/python3.10/site-packages/beets/ui/commands.py", line 1034, in import_func
    import_files(lib, paths, query)
  File "/home/arsaboo/.local/lib/python3.10/site-packages/beets/ui/commands.py", line 974, in import_files
    session.run()
  File "/home/arsaboo/.local/lib/python3.10/site-packages/beets/importer.py", line 340, in run
    pl.run_parallel(QUEUE_SIZE)
  File "/home/arsaboo/.local/lib/python3.10/site-packages/beets/util/pipeline.py", line 446, in run_parallel
    raise exc_info[1].with_traceback(exc_info[2])
  File "/home/arsaboo/.local/lib/python3.10/site-packages/beets/util/pipeline.py", line 314, in run
    for msg in _allmsgs(out):
  File "/home/arsaboo/.local/lib/python3.10/site-packages/beets/util/pipeline.py", line 467, in pull
    out = coro.send(msg)
  File "/home/arsaboo/.local/lib/python3.10/site-packages/beets/util/pipeline.py", line 193, in coro
    func(*(args + (task,)))
  File "/home/arsaboo/.local/lib/python3.10/site-packages/beets/importer.py", line 1376, in lookup_candidates
    task.lookup_candidates()
  File "/home/arsaboo/.local/lib/python3.10/site-packages/beets/importer.py", line 913, in lookup_candidates
    prop = autotag.tag_item(self.item, search_ids=self.search_ids)
  File "/home/arsaboo/.local/lib/python3.10/site-packages/beets/autotag/match.py", line 518, in tag_item
    for track_info in hooks.item_candidates(item, search_artist, search_title):
  File "/home/arsaboo/.local/lib/python3.10/site-packages/beets/plugins.py", line 573, in decorated
    for v in generator(*args, **kwargs):
  File "/home/arsaboo/.local/lib/python3.10/site-packages/beets/autotag/hooks.py", line 647, in item_candidates
    yield from plugins.item_candidates(item, artist, title)
  File "/home/arsaboo/.local/lib/python3.10/site-packages/beets/plugins.py", line 392, in item_candidates
    yield from plugin.item_candidates(item, artist, title)
  File "/home/arsaboo/.local/lib/python3.10/site-packages/beets/plugins.py", line 749, in item_candidates
    tracks = self._search_api(
  File "/home/arsaboo/.local/lib/python3.10/site-packages/beetsplug/spotify.py", line 400, in _search_api
    self._handle_response(
  File "/home/arsaboo/.local/lib/python3.10/site-packages/beetsplug/spotify.py", line 197, in _handle_response
    raise SpotifyAPIError("API Error: {}\nURL: {}\nparams: {}".
beetsplug.spotify.SpotifyAPIError: API Error: 404
URL: https://api.spotify.com/v1/search
params: {'q': 'Jiya Laage Na artist:Shankar Mahadevan, Khagan Gogoi, Megha Sriram Dalton, Akriti Kakar & Leslie Lewis', 'type': 'track'}
arsaboo@arsplex:~$

```
Any thoughts as to what may be going on and I can try to fix the issue?


Discussion:

Resolving patch:
diff --git a/beetsplug/spotify.py b/beetsplug/spotify.py
--- a/beetsplug/spotify.py
+++ b/beetsplug/spotify.py
@@ -194,8 +194,9 @@ def _handle_response(self, request_type, url, params=None):
                 time.sleep(int(seconds) + 1)
                 return self._handle_response(request_type, url, params=params)
             elif response.status_code == 404:
-                raise SpotifyAPIError("API Error {0.status_code} for {1}"
-                                      .format(response, url))
+                raise SpotifyAPIError("API Error: {}\nURL: {}\nparams: {}".
+                                      format(response.status_code, url,
+                                             params))
             else:
                 raise ui.UserError(
                     '{} API error:\n{}\nURL:\n{}\nparams:\n{}'.format(
@@ -395,15 +396,17 @@ def _search_api(self, query_type, filters=None, keywords=''):
         self._log.debug(
             f"Searching {self.data_source} for '{query}'"
         )
-        response_data = (
-            self._handle_response(
+        try:
+            response = self._handle_response(
                 requests.get,
                 self.search_url,
                 params={'q': query, 'type': query_type},
             )
-            .get(query_type + 's', {})
-            .get('items', [])
-        )
+        except SpotifyAPIError as e:
+            self._log.debug('Spotify API error: {}', e)
+            return []
+        response_data = (response.get(query_type + 's', {})
+                         .get('items', []))
         self._log.debug(
             "Found {} result(s) from {} for '{}'",
             len(response_data),


Test patch: