Commit 8dabbb82 authored by Bas de Nooijer's avatar Bas de Nooijer

- improved error detection and handling in client adapters

- fixed timeout setting for client adapters
- added an exception class for HTTP related exceptions
- improved phpdoc
parent db13b3c1
Solarium is a PHP Solr client that not only facilitates Solr communication but
also tries to accurately model Solr concepts.
Please see the wiki for a more detailed description.
For Docs, License, Issues, and pre-packed downloads, see:
Project page:
http://github.com/basdenooijer/solarium
License:
See the COPYING file or view online
https://github.com/basdenooijer/solarium/blob/master/COPYING
Wiki:
https://github.com/basdenooijer/solarium/wiki
Issue tracker:
http://github.com/basdenooijer/solarium/issues
Contributors:
http://github.com/basdenooijer/solarium/contributors
https://github.com/basdenooijer/solarium/contributors
API docs:
http://solarium.raspberry.nl/api/
\ No newline at end of file
......@@ -69,6 +69,7 @@ class Solarium_Client extends Solarium_Configurable
'path' => '/solr',
'core' => null,
'adapter' => 'Solarium_Client_Adapter_Http',
'timeout' => 5,
);
/**
......
......@@ -57,17 +57,6 @@
abstract class Solarium_Client_Adapter extends Solarium_Configurable
{
/**
* Default options
*
* @var array
*/
protected $_options = array(
'adapteroptions' => array(
'timeout' => 5
),
);
/**
* Set options
*
......
......@@ -44,15 +44,6 @@
class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter
{
/**
* Default options
*
* @var array
*/
protected $_options = array(
'timeout' => 5,
);
/**
* Executes a select query
*
......@@ -99,8 +90,6 @@ class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter
/**
* Handle Solr communication
*
* @todo check http response code
*
* @throws Solarium_Exception
* @param Solarium_Client_Request
* @return array
......@@ -135,6 +124,14 @@ class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter
$data = @file_get_contents($request->getUri(), false, $context);
// if there is no data and there are no headers it's a total failure,
// a connection to the host was impossible.
if (false === $data && !isset($http_response_header)) {
throw new Solarium_Client_HttpException("HTTP request failed");
}
$this->_checkHeaders($http_response_header);
if ($method == Solarium_Client_Request::HEAD) {
// HEAD request has no result data
return true;
......@@ -148,6 +145,46 @@ class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter
}
}
/**
* Check HTTP headers
*
* The status header is parsed, an exception will be thrown for an error
* code.
*
* @throws Solarium_Client_HttpException
* @param array $headers
* @return void
*/
protected function _checkHeaders($headers)
{
// get the status header
$statusHeader = null;
foreach( $headers AS $header) {
if (substr($header, 0, 4) == 'HTTP') {
$statusHeader = $header;
break;
}
}
if (null == $statusHeader) {
throw new Solarium_Client_HttpException("No HTTP status found");
}
// parse header like "$statusInfo[1]" into code and message
// $statusInfo[1] = the HTTP response code
// $statusInfo[2] = the response message
$statusInfo = explode(' ', $statusHeader, 3);
// check status for error (range of 400 and 500)
$statusNum = floor($statusInfo[1] / 100);
if ($statusNum == 4 || $statusNum == 5) {
throw new Solarium_Client_HttpException(
$statusInfo[2],
$statusInfo[1]
);
}
}
/**
* Decode json response data
*
......
......@@ -116,9 +116,12 @@ class Solarium_Client_Adapter_ZendHttp extends Solarium_Client_Adapter_Http
public function getZendHttp()
{
if (null == $this->_zendHttp) {
$options = null;
$options = array('timeout' => $this->getOption('timeout'));
if (isset($this->_options['adapteroptions'])) {
$options = $this->_options['adapteroptions'];
$options = array_merge(
$options,
$this->_options['adapteroptions']
);
}
$this->_zendHttp = new Zend_Http_Client(null, $options);
......@@ -143,6 +146,14 @@ class Solarium_Client_Adapter_ZendHttp extends Solarium_Client_Adapter_Http
$response = $client->request();
// throw an exception in case of a HTTP error
if ($response->isError()) {
throw new Solarium_Client_HttpException(
$response->getMessage(),
$response->getStatus()
);
}
if ($request->getMethod() == Solarium_Client_Request::HEAD) {
return true;
} else {
......
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
*
* @package Solarium
* @subpackage Client
*/
/**
* Solarium client HTTP exception
*
* This exception class exists to make it easy to catch HTTP errors.
* HTTP errors usually mean your Solr settings or Solr input (e.g. query)
* contain an error.
*
* The getCode method will return the HTTP response code returned by the server.
*
* The getMessage method will return the corresponding message, as returned by
* the server.
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_HttpException extends Solarium_Exception
{
/**
* HTTP status message
*
* @var string
*/
protected $_statusMessage;
/**
* Exception constructor
*
* The input message is a HTTP status message. Because an exception with the
* message 'Not Found' is not very clear it this message is tranformed to a
* more descriptive text. The original message is available using the
* {@link getStatusMessage} method.
*
* @param string $statusMessage
* @param int|null $code
*/
public function __construct($statusMessage, $code = null)
{
$this->_statusMessage = $statusMessage;
$message = 'Solr HTTP error, ' . $statusMessage;
if (null !== $code) {
$message .= ' (' . $code . ')';
}
parent::__construct($message, $code);
}
/**
* Get the HTTP status message
*
* @return string
*/
public function getStatusMessage()
{
return $this->_statusMessage;
}
}
\ No newline at end of file
......@@ -55,10 +55,10 @@ class Solarium_Escape
* A term is a single word.
* All characters that have a special meaning in a Solr query are escaped.
*
* @link http://lucene.apache.org/java/docs/queryparsersyntax.html#Escaping%20Special%20Characters
*
* If you want to use the input as a phrase please use the {@link phrase()}
* method, because a phrase requires much less escaping.
* method, because a phrase requires much less escaping.\
*
* @link http://lucene.apache.org/java/docs/queryparsersyntax.html#Escaping%20Special%20Characters
*
* @param string $input
* @return string
......
......@@ -80,6 +80,8 @@ class Solarium_Result_Query
/**
* Get Solr status code
*
* This is not the HTTP status code! The normal value for success is 0.
*
* @return int
*/
public function getStatus()
......
Markdown is supported
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