diff --git a/beetsplug/mbcollection.py b/beetsplug/mbcollection.py index 90e2d3578..9fb532978 100644 --- a/beetsplug/mbcollection.py +++ b/beetsplug/mbcollection.py @@ -23,6 +23,18 @@ from musicbrainzngs import musicbrainz SUBMISSION_CHUNK_SIZE = 200 +def mb_request(*args, **kwargs): + """Send a MusicBrainz API request and process exceptions. + """ + try: + return musicbrainz._mb_request(*args, **kwargs) + except musicbrainzngs.AuthenticationError: + raise ui.UserError('authentication with MusicBrainz failed') + except musicbrainzngs.ResponseError as exc: + raise ui.UserError('MusicBrainz API error: {0}'.format(exc)) + except musicbrainzngs.UsageError: + raise ui.UserError('MusicBrainz credentials missing') + def submit_albums(collection_id, release_ids): """Add all of the release IDs to the indicated collection. Multiple requests are made if there are many release IDs to submit. @@ -30,15 +42,16 @@ def submit_albums(collection_id, release_ids): for i in range(0, len(release_ids), SUBMISSION_CHUNK_SIZE): chunk = release_ids[i:i+SUBMISSION_CHUNK_SIZE] releaselist = ";".join(chunk) - musicbrainz._mb_request( + mb_request( "collection/%s/releases/%s" % (collection_id, releaselist), - 'PUT', True, True, body='foo') + 'PUT', True, True, body='foo' + ) # A non-empty request body is required to avoid a 411 "Length # Required" error from the MB server. def update_collection(lib, opts, args): # Get the collection to modify. - collections = musicbrainz._mb_request('collection', 'GET', True, True) + collections = mb_request('collection', 'GET', True, True) if not collections['collection-list']: raise ui.UserError('no collections exist for user') collection_id = collections['collection-list'][0]['id'] @@ -60,7 +73,7 @@ class MusicBrainzCollectionPlugin(BeetsPlugin): super(MusicBrainzCollectionPlugin, self).__init__() musicbrainzngs.auth( config['musicbrainz']['user'].get(unicode), - config['musicbrainz']['pass'].get(unicode) + config['musicbrainz']['pass'].get(unicode), ) def commands(self): diff --git a/docs/changelog.rst b/docs/changelog.rst index 4deda0c6f..f7ad1b07b 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -38,6 +38,8 @@ Other stuff: pathnames. * Fix a spurious warning from the Unidecode module when matching albums that are missing all metadata. +* :doc:`/plugins/mbcollection`: Show friendly, human-readable errors when + MusicBrainz exceptions occur. 1.1b2 (February 16, 2013) -------------------------