From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-2.9 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, URIBL_BLOCKED shortcircuit=no autolearn=unavailable version=3.3.2 X-Original-To: meta@public-inbox.org Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 8F290200F4 for ; Sat, 5 Sep 2015 09:01:11 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 3/6] searchview: error description for invalid queries Date: Sat, 5 Sep 2015 09:01:05 +0000 Message-Id: <1441443668-21092-4-git-send-email-e@80x24.org> In-Reply-To: <1441443668-21092-1-git-send-email-e@80x24.org> References: <1441443668-21092-1-git-send-email-e@80x24.org> List-Id: Xapian may raise exceptions on some queries. Pass the error along to the user so they can read Xapian documentation. --- lib/PublicInbox/SearchView.pm | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/PublicInbox/SearchView.pm b/lib/PublicInbox/SearchView.pm index 960049f..96b691d 100644 --- a/lib/PublicInbox/SearchView.pm +++ b/lib/PublicInbox/SearchView.pm @@ -12,13 +12,18 @@ our $LIM = 25; sub sres_top_html { my ($ctx, $q) = @_; my $cgi = $ctx->{cgi}; + my $code = 200; # $q ||= $cgi->param('q'); my $o = int($cgi->param('o') || 0); my $r = $cgi->param('r'); $r = (defined $r && $r ne '0'); my $opts = { limit => $LIM, offset => $o, mset => 1, relevance => $r }; - my $mset = $ctx->{srch}->query($q, $opts); - my $total = $mset->get_matches_estimated; + my ($mset, $total); + eval { + $mset = $ctx->{srch}->query($q, $opts); + $total = $mset->get_matches_estimated; + }; + my $err = $@; my $query = PublicInbox::Hval->new_oneline($q); my $qh = $query->as_html; my $res = "$qh - search results" . @@ -32,7 +37,16 @@ sub sres_top_html { my $foot = $ctx->{footer} || ''; $foot = qq{Back to index.}; - if ($total == 0) { + if ($err) { + my $u = 'http://xapian.org/docs/queryparser.html'; + $code = 400; + $err =~ s/^\s*Exception:\s*//; # bad word to show users :P + $err = PublicInbox::Hval->new_oneline($err)->as_html; + $res .= "\n\nBad query: $err\n"; + $res .= qq{See $u for Xapian query syntax}; + $res .= "
$foot";
+	} elsif ($total == 0) {
+		$code = 404;
 		$res .= "\n\n[No results found]

$foot";
 	} else {
 		$q = $query->as_href;
@@ -74,7 +88,7 @@ sub sres_top_html {
 	}
 
 	$res .= "
"; - [200, ['Content-Type'=>'text/html; charset=UTF-8'], [$res]]; + [$code, ['Content-Type'=>'text/html; charset=UTF-8'], [$res]]; } sub dump_mset { -- EW