# Buggy code
def album_for_id(self, album_id):
    """Fetches an album by its Discogs ID and returns an AlbumInfo object
    or None if the album is not found.
    """
    if not self.discogs_client:
        return

    self._log.debug("Searching for release {0}", album_id)
    # Discogs-IDs are simple integers. We only look for those at the end
    # of an input string as to avoid confusion with other metadata plugins.
    # An optional bracket can follow the integer, as this is how discogs
    # displays the release ID on its webpage.
    match = re.search(r"(^|\[*r|discogs\.com/.+/release/)(\d+)($|\])", album_id)
    if not match:
        return None
    result = Release(self.discogs_client, {"id": int(match.group(2))})
    # Try to obtain title to verify that we indeed have a valid Release
    try:
        getattr(result, "title")
    except DiscogsAPIError as e:
        if e.status_code != 404:
            self._log.debug("API Error: {0} (query: {1})", e, result._uri)
            if e.status_code == 401:
                self.reset_auth()
                return self.album_for_id(album_id)
        return None
    except CONNECTION_ERRORS:
        self._log.debug("Connection error in album lookup", exc_info=True)
        return None
    return self.get_album_info(result)


# Traceback (discogs_client.exceptions.HTTPError)
File "/beets/beetsplug/discogs.py", line 257, in get_master_year
year = result.fetch('year')
File "/usr/lib/python3.7/site-packages/discogs_client/models.py", line 245, in fetch
self.refresh()
File "/usr/lib/python3.7/site-packages/discogs_client/models.py", line 211, in refresh
data = self.client._get(self.data['resource_url'])
File "/usr/lib/python3.7/site-packages/discogs_client/client.py", line 123, in _get
return self._request('GET', url)
File "/usr/lib/python3.7/site-packages/discogs_client/client.py", line 120, in _request
raise HTTPError(body['message'], status_code)
discogs_client.exceptions.HTTPError: 502: The Discogs API is undergoing maintenance. We'll be back in a moment.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/bin/beet", line 11, in <module>
load_entry_point('beets', 'console_scripts', 'beet')()
File "/beets/beets/ui/__init__.py", line 1266, in main
_raw_main(args)
File "/beets/beets/ui/__init__.py", line 1253, in _raw_main
subcommand.func(lib, suboptions, subargs)
File "/beets/beets/ui/commands.py", line 955, in import_func
import_files(lib, paths, query)
File "/beets/beets/ui/commands.py", line 925, in import_files
session.run()
File "/beets/beets/importer.py", line 329, in run
pl.run_parallel(QUEUE_SIZE)
File "/beets/beets/util/pipeline.py", line 445, in run_parallel
six.reraise(exc_info[0], exc_info[1], exc_info[2])
File "/usr/lib/python3.7/site-packages/six.py", line 693, in reraise
raise value
File "/beets/beets/util/pipeline.py", line 312, in run
out = self.coro.send(msg)
File "/beets/beets/util/pipeline.py", line 194, in coro
func(*(args + (task,)))
File "/beets/beets/importer.py", line 1351, in lookup_candidates
task.lookup_candidates()
File "/beets/beets/importer.py", line 641, in lookup_candidates
autotag.tag_album(self.items, search_ids=self.search_ids)
File "/beets/beets/autotag/match.py", line 460, in tag_album
va_likely):
File "/beets/beets/plugins.py", line 571, in decorated
for v in generator(*args, **kwargs):
File "/beets/beets/autotag/hooks.py", line 620, in album_candidates
for candidate in plugins.candidates(items, artist, album, va_likely):
File "/beets/beets/plugins.py", line 381, in candidates
for candidate in plugin.candidates(items, artist, album, va_likely):
File "/beets/beetsplug/discogs.py", line 176, in candidates
return self.get_albums(query)
File "/beets/beetsplug/discogs.py", line 245, in get_albums
return [album for album in map(self.get_album_info, releases[:5])
File "/beets/beetsplug/discogs.py", line 245, in <listcomp>
return [album for album in map(self.get_album_info, releases[:5])
File "/beets/beetsplug/discogs.py", line 335, in get_album_info
original_year = self.get_master_year(master_id) if master_id else year
File "/beets/beetsplug/discogs.py", line 262, in get_master_year
self._log.debug(u'API Error: {0} (query: {1})', e, result._uri)
AttributeError: 'Master' object has no attribute '_uri'

# Fixed code
def album_for_id(self, album_id):
    """Fetches an album by its Discogs ID and returns an AlbumInfo object
    or None if the album is not found.
    """
    if not self.discogs_client:
        return

    self._log.debug("Searching for release {0}", album_id)
    # Discogs-IDs are simple integers. We only look for those at the end
    # of an input string as to avoid confusion with other metadata plugins.
    # An optional bracket can follow the integer, as this is how discogs
    # displays the release ID on its webpage.
    match = re.search(r"(^|\[*r|discogs\.com/.+/release/)(\d+)($|\])", album_id)
    if not match:
        return None
    result = Release(self.discogs_client, {"id": int(match.group(2))})
    # Try to obtain title to verify that we indeed have a valid Release
    try:
        getattr(result, "title")
    except DiscogsAPIError as e:
        if e.status_code != 404:
            self._log.debug(
                "API Error: {0} (query: {1})", e, result.data["resource_url"]
            )
            if e.status_code == 401:
                self.reset_auth()
                return self.album_for_id(album_id)
        return None
    except CONNECTION_ERRORS:
        self._log.debug("Connection error in album lookup", exc_info=True)
        return None
    return self.get_album_info(result)