Adrian Sampson avatar Adrian Sampson committed 1e92de2

mbcollection: human-readable MB exceptions

Fixes #107 by wrapping the API invocation function with exception handlers.

Comments (0)

Files changed (2)

beetsplug/mbcollection.py

 
 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.
     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']
         super(MusicBrainzCollectionPlugin, self).__init__()
         musicbrainzngs.auth(
             config['musicbrainz']['user'].get(unicode),
-            config['musicbrainz']['pass'].get(unicode)
+            config['musicbrainz']['pass'].get(unicode),
         )
 
     def commands(self):

docs/changelog.rst

   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)
 -------------------------
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.