Skip to content
Snippets Groups Projects
Commit 753eed65 authored by Demian Katz's avatar Demian Katz
Browse files

Added Solr 4 error listener.

parent 0ff2d27c
No related merge requests found
<?php
/**
* SOLR 4.x error listener.
*
* PHP version 5
*
* Copyright (C) Villanova University 2013.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @category VuFind2
* @package Search
* @author David Maus <maus@hab.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org Main Site
*/
namespace VuFind\Search\Solr\V4;
use VuFindSearch\Backend\Exception\HttpErrorException;
use Zend\EventManager\EventInterface;
/**
* SOLR 3.x error listener.
*
* @category VuFind2
* @package Search
* @author David Maus <maus@hab.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org Main Site
*/
class ErrorListener
{
/**
* Backends ot listen on.
*
* @var array
*/
protected $backends;
/**
* Constructor.
*
* @param array $backends Name of backends to listen on
*
* @return void
*/
public function __construct(array $backends)
{
$this->backends = $backends;
}
/**
* VuFindSearch.error
*
* @param EventInterface $event Event
*
* @return EventInterface
*/
public function onSearchError(EventInterface $event)
{
$backend = $event->getParam('backend');
if (in_array($backend, $this->backends)) {
$error = $event->getTarget();
if ($error instanceOf HttpErrorException) {
$body = $error->getResponse()->getBody();
$type = $error->getResponse()->getHeaders()->get('content-type')
->toString();
if (stristr($type, 'json')) {
$body = json_decode($body);
$reason = isset($body->error->msg) ? $body->error->msg : '';
} else if (stristr($type, 'xml')) {
// TODO -- parse XML response
$reason = '';
} else {
$reason = '';
}
if (stristr($reason, 'org.apache.solr.search.SyntaxError')
|| stristr($reason, 'undefined field')
) {
$error->addTag('VuFind\Search\ParserError');
}
}
}
return $event;
}
}
\ No newline at end of file
HTTP/1.1 500 Server Error
Cache-Control: no-cache, no-store
Pragma: no-cache
Expires: Sat, 01 Jan 2000 01:00:00 GMT
Last-Modified: Wed, 29 May 2013 15:35:01 GMT
ETag: "13ef0ed92c7"
Content-Type: application/json; charset=UTF-8
{"responseHeader":{"status":500,"QTime":135},"error":{"msg":"I couldn't find a browse index at: /home/dkatz/vufind3/solr/alphabetical_browse/author_browse.db.\nMaybe you need to create your browse indexes?","trace":"java.lang.Exception: I couldn't find a browse index at: /home/dkatz/vufind3/solr/alphabetical_browse/author_browse.db.\nMaybe you need to create your browse indexes?\n\tat au.gov.nla.solr.handler.HeadingsDB.openDB(BrowseRequestHandler.java:72)\n\tat au.gov.nla.solr.handler.HeadingsDB.reopenIfUpdated(BrowseRequestHandler.java:127)\n\tat au.gov.nla.solr.handler.Browse.reopenDatabasesIfUpdated(BrowseRequestHandler.java:553)\n\tat au.gov.nla.solr.handler.BrowseRequestHandler.handleRequestBody(BrowseRequestHandler.java:752)\n\tat org.apache.solr.handler.RequestHandlerBase.handleRequest(RequestHandlerBase.java:135)\n\tat org.apache.solr.core.SolrCore.execute(SolrCore.java:1817)\n\tat org.apache.solr.servlet.SolrDispatchFilter.execute(SolrDispatchFilter.java:639)\n\tat org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:345)\n\tat org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:141)\n\tat org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1307)\n\tat org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:453)\n\tat org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)\n\tat org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:560)\n\tat org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)\n\tat org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1072)\n\tat org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:382)\n\tat org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)\n\tat org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1006)\n\tat org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)\n\tat org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:255)\n\tat org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:154)\n\tat org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)\n\tat org.eclipse.jetty.server.Server.handle(Server.java:365)\n\tat org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:485)\n\tat org.eclipse.jetty.server.BlockingHttpConnection.handleRequest(BlockingHttpConnection.java:53)\n\tat org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:926)\n\tat org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:988)\n\tat org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:635)\n\tat org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)\n\tat org.eclipse.jetty.server.BlockingHttpConnection.handle(BlockingHttpConnection.java:72)\n\tat org.eclipse.jetty.server.bio.SocketConnector$ConnectorEndPoint.run(SocketConnector.java:264)\n\tat org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)\n\tat org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)\n\tat java.lang.Thread.run(Thread.java:679)\n","code":500}}
HTTP/1.1 400 Bad Request
Cache-Control: no-cache, no-store
Pragma: no-cache
Expires: Sat, 01 Jan 2000 01:00:00 GMT
Last-Modified: Wed, 29 May 2013 15:31:45 GMT
ETag: "13ef0ea94ef"
Content-Type: application/json; charset=UTF-8
{"responseHeader":{"status":400,"QTime":2,"params":{"spellcheck":"true","facet":"true","sort":"score desc","facet.mincount":"1","spellcheck.q":"~~~","facet.limit":"30","hl.simple.pre":"{{{{START_HILITE}}}}","json.nl":"arrarr","hl.fl":"*","wt":"json","hl":"true","rows":"20","fl":"*,score","start":"0","facet.sort":"count","q":"((title_short:(\"~~~\")^750 OR title_full_unstemmed:(\"~~~\")^600 OR title_full_unstemmed:(~~~)^500 OR title_full:(\"~~~\")^400 OR title:(\"~~~\")^300 OR title:(~~~)^250 OR title_alt:(~~~)^200 OR title_new:(~~~)^100)^50 OR series:(~~~)^50 OR series2:(~~~)^30 OR author:(\"~~~\")^300 OR author:(~~~)^250 OR author_fuller:(\"~~~\")^150 OR author_fuller:(~~~)^125 OR author2:(~~~)^50 OR author_additional:(~~~)^50 OR contents:(~~~)^10 OR topic_unstemmed:(\"~~~\")^550 OR topic_unstemmed:(~~~)^500 OR topic:(\"~~~\")^500 OR geographic:(\"~~~\")^300 OR genre:(\"~~~\")^300 OR allfields_unstemmed:(~~~)^10 OR fulltext_unstemmed:(~~~)^10 OR allfields:(~~~) OR fulltext:(~~~))","spellcheck.dictionary":"default","hl.simple.post":"{{{{END_HILITE}}}}","facet.field":["topic_facet","institution","building","format","callnumber-first","publishDate","authorStr","language","genre_facet","era_facet","geographic_facet"]}},"error":{"msg":"org.apache.solr.search.SyntaxError: Cannot parse '((title_short:(\"~~~\")^750 OR title_full_unstemmed:(\"~~~\")^600 OR title_full_unstemmed:(~~~)^500 OR title_full:(\"~~~\")^400 OR title:(\"~~~\")^300 OR title:(~~~)^250 OR title_alt:(~~~)^200 OR title_new:(~~~)^100)^50 OR series:(~~~)^50 OR series2:(~~~)^30 OR author:(\"~~~\")^300 OR author:(~~~)^250 OR author_fuller:(\"~~~\")^150 OR author_fuller:(~~~)^125 OR author2:(~~~)^50 OR author_additional:(~~~)^50 OR contents:(~~~)^10 OR topic_unstemmed:(\"~~~\")^550 OR topic_unstemmed:(~~~)^500 OR topic:(\"~~~\")^500 OR geographic:(\"~~~\")^300 OR genre:(\"~~~\")^300 OR allfields_unstemmed:(~~~)^10 OR fulltext_unstemmed:(~~~)^10 OR allfields:(~~~) OR fulltext:(~~~))': Encountered \" <FUZZY_SLOP> \"~ \"\" at line 1, column 91.\nWas expecting one of:\n <NOT> ...\n \"+\" ...\n \"-\" ...\n <BAREOPER> ...\n \"(\" ...\n \"*\" ...\n <QUOTED> ...\n <TERM> ...\n <PREFIXTERM> ...\n <WILDTERM> ...\n <REGEXPTERM> ...\n \"[\" ...\n \"{\" ...\n <LPARAMS> ...\n <NUMBER> ...\n <TERM> ...\n \"*\" ...\n ","code":400}}
HTTP/1.1 400 Bad Request
Cache-Control: no-cache, no-store
Pragma: no-cache
Expires: Sat, 01 Jan 2000 01:00:00 GMT
Last-Modified: Wed, 29 May 2013 15:30:38 GMT
ETag: "13ef0e9905e"
Content-Type: application/json; charset=UTF-8
{"responseHeader":{"status":400,"QTime":2,"params":{"spellcheck":"true","facet":"true","sort":"score desc","facet.mincount":"1","spellcheck.q":"test:test","facet.limit":"30","hl.simple.pre":"{{{{START_HILITE}}}}","json.nl":"arrarr","hl.fl":"*","wt":"json","hl":"true","rows":"20","fl":"*,score","start":"0","facet.sort":"count","q":"test:test","spellcheck.dictionary":"default","hl.simple.post":"{{{{END_HILITE}}}}","facet.field":["topic_facet","institution","building","format","callnumber-first","publishDate","authorStr","language","genre_facet","era_facet","geographic_facet"]}},"error":{"msg":"undefined field test","code":400}}
<?php
/**
* Unit tests for SOLR 3.x error listener.
*
* PHP version 5
*
* Copyright (C) Villanova University 2013.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* @category VuFind2
* @package Search
* @author David Maus <maus@hab.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org Main Site
*/
namespace VuFindTest\Search\Solr\V4;
use VuFind\Search\Solr\V4\ErrorListener;
use VuFindSearch\Backend\Exception\HttpErrorException;
use Zend\EventManager\Event;
use Zend\Http\Response;
use PHPUnit_Framework_TestCase as TestCase;
use RuntimeException;
/**
* Unit tests for SOLR 3.x error listener.
*
* @category VuFind2
* @package Search
* @author David Maus <maus@hab.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link http://vufind.org Main Site
*/
class ErrorListenerTest extends TestCase
{
/**
* Detect parser error response.
*
* @return void
*/
public function testDetectParseError()
{
$response = $this->createResponse('solr4-parse-error');
$exception = HttpErrorException::createFromResponse($response);
$params = array('backend' => 'test');
$event = new Event(null, $exception, $params);
$listener = new ErrorListener(array('test'));
$listener->onSearchError($event);
$this->assertTrue($exception->hasTag('VuFind\Search\ParserError'));
}
/**
* Detect parser error response.
*
* @return void
*/
public function testDetectUndefinedFieldError()
{
$response = $this->createResponse('solr4-undefined-field-error');
$exception = HttpErrorException::createFromResponse($response);
$params = array('backend' => 'test');
$event = new Event(null, $exception, $params);
$listener = new ErrorListener(array('test'));
$listener->onSearchError($event);
$this->assertTrue($exception->hasTag('VuFind\Search\ParserError'));
}
/// Internal API
/**
* Return response fixture
*
* @param string $name Name of fixture
*
* @return Response Response
*/
protected function createResponse($name)
{
$file = realpath(
\VUFIND_PHPUNIT_MODULE_PATH . '/fixtures/response/solr/' . $name
);
if (!$file) {
throw new RuntimeException(
sprintf('Unable to resolve fixture to fixture file: %s', $name)
);
}
$response = Response::fromString(file_get_contents($file));
return $response;
}
}
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment