mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-07 10:14:46 -04:00
Nicer error message for invalid search queries to calibredb search
This commit is contained in:
parent
2196b708fa
commit
50557eb1ae
@ -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)
|
||||
|
@ -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']
|
||||
|
||||
|
@ -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}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user