diff --git a/src/calibre/db/cli/cmd_search.py b/src/calibre/db/cli/cmd_search.py index e386077b9a..246e230735 100644 --- a/src/calibre/db/cli/cmd_search.py +++ b/src/calibre/db/cli/cmd_search.py @@ -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) diff --git a/src/calibre/db/cli/main.py b/src/calibre/db/cli/main.py index d3380341c7..1a1788f0e1 100644 --- a/src/calibre/db/cli/main.py +++ b/src/calibre/db/cli/main.py @@ -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'] diff --git a/src/calibre/srv/cdb.py b/src/calibre/srv/cdb.py index 333a31d096..02842ca915 100644 --- a/src/calibre/srv/cdb.py +++ b/src/calibre/srv/cdb.py @@ -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}