From f443e0bfc5d1114b1dbc182ea223aab23d9f6a49 Mon Sep 17 00:00:00 2001 From: Bruno Cauet Date: Mon, 9 Feb 2015 15:44:49 +0100 Subject: [PATCH] InvalidQueryArgumentTypeError does not extend InvalidQueryError Places where InvalidQueryArgumentTypeError may be raised (i.e. all current ones) may not know the query therefore it cannot be an InvalidQueryError. The InvalidQueryArgumentTypeError is caught in beets.library.Library._fetch() and an InvalidQueryError is then raised. Improve #1290. --- beets/dbcore/query.py | 17 ++++++++++++++--- beets/library.py | 13 ++++++++----- test/test_query.py | 4 ++-- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py index 8d9c1929c..3727f6d7f 100644 --- a/beets/dbcore/query.py +++ b/beets/dbcore/query.py @@ -24,17 +24,28 @@ from datetime import datetime, timedelta class InvalidQueryError(ValueError): + """Represent any kind of invalid query + + The query should be a unicode string or a list, which will be space-joined. + """ def __init__(self, query, explanation): - message = "Invalid query '{0}': {1}".format(query, explanation) + if isinstance(query, list): + query = " ".join(query) + message = "'{0}': {1}".format(query, explanation) super(InvalidQueryError, self).__init__(message) -class InvalidQueryArgumentTypeError(InvalidQueryError, TypeError): +class InvalidQueryArgumentTypeError(TypeError): + """Represent a query argument that could not be converted as expected. + + It exists to be caught in upper stack levels so a meaningful (i.e. with the + query) InvalidQueryError can be raised. + """ def __init__(self, what, expected, detail=None): message = "'{0}' is not {1}".format(what, expected) if detail: message = "{0}: {1}".format(message, detail) - super(InvalidQueryArgumentTypeError, self).__init__(None, message) + super(InvalidQueryArgumentTypeError, self).__init__(message) class Query(object): diff --git a/beets/library.py b/beets/library.py index a9885918b..a57ed1642 100644 --- a/beets/library.py +++ b/beets/library.py @@ -1155,11 +1155,14 @@ class Library(dbcore.Database): in the query string the `sort` argument is ignored. """ # Parse the query, if necessary. - parsed_sort = None - if isinstance(query, basestring): - query, parsed_sort = parse_query_string(query, model_cls) - elif isinstance(query, (list, tuple)): - query, parsed_sort = parse_query_parts(query, model_cls) + try: + parsed_sort = None + if isinstance(query, basestring): + query, parsed_sort = parse_query_string(query, model_cls) + elif isinstance(query, (list, tuple)): + query, parsed_sort = parse_query_parts(query, model_cls) + except dbcore.query.InvalidQueryArgumentTypeError as exc: + raise dbcore.InvalidQueryError(query, exc) # Any non-null sort specified by the parsed query overrides the # provided sort. diff --git a/test/test_query.py b/test/test_query.py index 077518c36..a32d8d60d 100644 --- a/test/test_query.py +++ b/test/test_query.py @@ -23,7 +23,7 @@ from test import helper import beets.library from beets import dbcore -from beets.dbcore import types, InvalidQueryError +from beets.dbcore import types from beets.dbcore.query import NoneQuery, InvalidQueryArgumentTypeError from beets.library import Library, Item @@ -290,7 +290,7 @@ class GetTest(DummyDataTestCase): dbcore.query.RegexpQuery('year', '199(') self.assertIn('not a regular expression', unicode(raised.exception)) self.assertIn('unbalanced parenthesis', unicode(raised.exception)) - self.assertIsInstance(raised.exception, (InvalidQueryError, TypeError)) + self.assertIsInstance(raised.exception, TypeError) class MatchTest(_common.TestCase):