From: William Morgan Date: Mon, 18 May 2009 15:34:57 +0000 (-0400) Subject: make parse_user_query_string raise exceptions on error X-Git-Url: https://git.notmuchmail.org/git?a=commitdiff_plain;h=f35f205192ee39133633457c652e0c05a45386c9;p=sup make parse_user_query_string raise exceptions on error I.e. instead of flashing an error message. This makes it usable from non-curses contexts, e.g. sup-tweak-labels. --- diff --git a/lib/sup/index.rb b/lib/sup/index.rb index 7642e08..8c9ddc8 100644 --- a/lib/sup/index.rb +++ b/lib/sup/index.rb @@ -485,8 +485,12 @@ EOS protected - ## do any specialized parsing - ## returns nil and flashes error message if parsing failed + class ParseError < StandardError; end + + ## parse a query string from the user. returns a query object and a set of + ## extra flags; both of these are meant to be passed to #build_query. + ## + ## raises a ParseError if something went wrong. def parse_user_query_string s extraopts = {} @@ -548,11 +552,9 @@ protected end if $have_chronic - chronic_failure = false subs = subs.gsub(/\b(before|on|in|during|after):(\((.+?)\)\B|(\S+)\b)/) do - break if chronic_failure field, datestr = $1, ($3 || $4) - realdate = Chronic.parse(datestr, :guess => false, :context => :past) + realdate = Chronic.parse datestr, :guess => false, :context => :past if realdate case field when "after" @@ -566,11 +568,9 @@ protected "date:(<= #{sprintf "%012d", realdate.end.to_i}) date:(>= #{sprintf "%012d", realdate.begin.to_i})" end else - BufferManager.flash "Can't understand date #{datestr.inspect}!" - chronic_failure = true + raise ParseError, "can't understand date #{datestr.inspect}" end end - subs = nil if chronic_failure end ## limit:42 restrict the search to 42 results @@ -580,15 +580,14 @@ protected extraopts[:limit] = lim.to_i '' else - BufferManager.flash "Can't understand limit #{lim.inspect}!" - subs = nil + raise ParseError, "non-numeric limit #{lim.inspect}" end end - if subs + begin [@qparser.parse(subs), extraopts] - else - nil + rescue Ferret::QueryParser::QueryParseException => e + raise ParseError, e.message end end diff --git a/lib/sup/modes/search-results-mode.rb b/lib/sup/modes/search-results-mode.rb index 6fdc58a..227ee9b 100644 --- a/lib/sup/modes/search-results-mode.rb +++ b/lib/sup/modes/search-results-mode.rb @@ -32,8 +32,8 @@ class SearchResultsMode < ThreadIndexMode mode = SearchResultsMode.new qobj, extraopts BufferManager.spawn "search: \"#{short_text}\"", mode mode.load_threads :num => mode.buffer.content_height - rescue Ferret::QueryParser::QueryParseException => e - BufferManager.flash "Couldn't parse query." + rescue Index::ParseError => e + BufferManager.flash "Problem: #{e.message}!" end end end