Nicer error message for invalid search queries to calibredb search

This commit is contained in:
Kovid Goyal 2020-09-15 06:47:13 +05:30
parent 2196b708fa
commit 50557eb1ae
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 22 additions and 7 deletions

View File

@ -10,7 +10,13 @@ version = 0 # change this if you change signature of implementation()
def implementation(db, notify_changes, query):
return db.search(query)
from calibre.utils.search_query_parser import ParseException
try:
return db.search(query)
except ParseException as err:
e = ValueError(_('Failed to parse search query: ({0}) with error: {1}').format(query, err))
e.suppress_traceback = True
raise e from err
def option_parser(get_parser, args):
@ -24,9 +30,9 @@ list of book ids matching the search expression. The output format is useful
to feed into other commands that accept a list of ids as input.
The search expression can be anything from calibre's powerful search query
language, for example: {0}
language, for example: %prog search {0}
'''
).format('author:asimov title:robot')
).format('author:asimov \'title:"i robot"\'')
)
parser.add_option(
'-l',
@ -42,7 +48,12 @@ def main(opts, args, dbctx):
if len(args) < 1:
raise SystemExit(_('Error: You must specify the search expression'))
q = ' '.join(args)
ids = dbctx.run('search', q)
try:
ids = dbctx.run('search', q)
except Exception as e:
if getattr(e, 'suppress_traceback', False):
raise SystemExit(str(e))
raise
if not ids:
raise SystemExit(_('No books matching the search expression:') + ' ' + q)
ids = sorted(ids)

View File

@ -213,7 +213,8 @@ class DBCtx(object):
self.interpret_http_error(err)
raise
if 'err' in ans:
prints(ans['tb'])
if ans['tb']:
prints(ans['tb'])
raise SystemExit(ans['err'])
return ans['result']

View File

@ -56,8 +56,11 @@ def cdb_run(ctx, rd, which, version):
try:
result = m.implementation(db, partial(ctx.notify_changes, db.backend.library_path), *args)
except Exception as err:
import traceback
return {'err': as_unicode(err), 'tb': traceback.format_exc()}
tb = ''
if not getattr(err, 'suppress_traceback', False):
import traceback
tb = traceback.format_exc()
return {'err': as_unicode(err), 'tb': tb}
return {'result': result}