Nicer error message when unknown location specified in search query

This commit is contained in:
Kovid Goyal 2023-08-25 14:34:39 +05:30
parent 0bbc2ad734
commit 8a9306d1bf
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C

View file

@ -123,6 +123,15 @@ class Token(NamedTuple):
REPLACEMENTS = tuple(('\\' + x, chr(i + 1)) for i, x in enumerate('\\"()'))
class NoLocation(ParseException):
def __init__(self, tt: str):
a, sep, b = tt.partition(':')
if sep == ':':
super().__init__(f'{a} is not a recognized location in {tt}')
else:
super().__init__(f'No location specified before {tt}')
class Parser:
def __init__(self, allow_no_location: bool = False) -> None:
@ -225,7 +234,7 @@ def base_token(self) -> SearchTreeNode:
assert tt is not None
if self.allow_no_location:
return TokenNode('all', tt)
raise ParseException(f'No location specified before {tt}')
raise NoLocation(tt)
tt = self.token(advance=True)
assert tt is not None
@ -253,7 +262,7 @@ def base_token(self) -> SearchTreeNode:
if self.allow_no_location:
return TokenNode('all', ':'.join(words))
raise ParseException(f'No location specified before {tt}')
raise NoLocation(tt)
@lru_cache(maxsize=64)