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.
This commit is contained in:
Bruno Cauet 2015-02-09 15:44:49 +01:00
parent 54887e7655
commit f443e0bfc5
3 changed files with 24 additions and 10 deletions

View file

@ -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):

View file

@ -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.

View file

@ -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):