2013-12-17 11:09:58 +01:00
|
|
|
Suppressing Server Errors
|
2014-03-31 11:59:37 +02:00
|
|
|
=========================
|
2013-12-17 11:09:58 +01:00
|
|
|
|
|
|
|
By default, exceptions from the Elastica client library will propagate through
|
|
|
|
the bundle's Client class. For instance, if the Elasticsearch server is offline,
|
|
|
|
issuing a request will result in an `Elastica\Exception\Connection` being thrown.
|
|
|
|
Depending on your needs, it may be desirable to suppress these exceptions and
|
|
|
|
allow searches to fail silently.
|
|
|
|
|
|
|
|
One way to achieve this is to override the `fos_elastica.client.class` service
|
|
|
|
container parameter with a custom class. In the following example, we override
|
|
|
|
the `Client::request()` method and return the equivalent of an empty search
|
|
|
|
response if an exception occurred.
|
|
|
|
|
2014-03-31 11:59:37 +02:00
|
|
|
Sample client code:
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
```php
|
2013-12-17 11:09:58 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Acme\ElasticaBundle;
|
|
|
|
|
|
|
|
use Elastica\Exception\ExceptionInterface;
|
2014-03-31 11:59:37 +02:00
|
|
|
use Elastica\Request;
|
2013-12-17 11:09:58 +01:00
|
|
|
use Elastica\Response;
|
2014-03-31 11:59:37 +02:00
|
|
|
use FOS\ElasticaBundle\Client as BaseClient;
|
2013-12-17 11:09:58 +01:00
|
|
|
|
|
|
|
class Client extends BaseClient
|
|
|
|
{
|
2014-03-31 11:59:37 +02:00
|
|
|
public function request($path, $method = Request::GET, $data = array(), array $query = array())
|
2013-12-17 11:09:58 +01:00
|
|
|
{
|
|
|
|
try {
|
2014-03-31 11:59:37 +02:00
|
|
|
return parent::request($path, $method, $data, $query);
|
2013-12-17 11:09:58 +01:00
|
|
|
} catch (ExceptionInterface $e) {
|
2014-05-23 14:57:57 +02:00
|
|
|
if ($this->_logger) {
|
|
|
|
$this->_logger->warning('Failed to send a request to ElasticSearch', array(
|
|
|
|
'exception' => $e->getMessage(),
|
|
|
|
'path' => $path,
|
|
|
|
'method' => $method,
|
|
|
|
'data' => $data,
|
|
|
|
'query' => $query
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2013-12-17 11:09:58 +01:00
|
|
|
return new Response('{"took":0,"timed_out":false,"hits":{"total":0,"max_score":0,"hits":[]}}');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-31 11:59:37 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
Configuration change:
|
|
|
|
---------------------
|
|
|
|
|
2014-05-23 14:57:57 +02:00
|
|
|
You must update a parameter in your `app/config/config.yml` file to point to your overridden client:
|
2014-03-31 11:59:37 +02:00
|
|
|
|
2014-05-23 14:57:57 +02:00
|
|
|
```yaml
|
|
|
|
parameters:
|
|
|
|
fos_elastica.client.class: Acme\ElasticaBundle\Client
|
|
|
|
```
|