mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-04 20:27:31 +02:00
More detailed ElasticsearchHandler exceptions
This commit is contained in:
@@ -11,14 +11,14 @@
|
|||||||
|
|
||||||
namespace Monolog\Handler;
|
namespace Monolog\Handler;
|
||||||
|
|
||||||
use Elasticsearch\Client;
|
|
||||||
use Elasticsearch\Common\Exceptions\RuntimeException as ElasticsearchRuntimeException;
|
|
||||||
use InvalidArgumentException;
|
|
||||||
use Monolog\Formatter\ElasticsearchFormatter;
|
|
||||||
use Monolog\Formatter\FormatterInterface;
|
|
||||||
use Monolog\Logger;
|
|
||||||
use RuntimeException;
|
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
use RuntimeException;
|
||||||
|
use Monolog\Logger;
|
||||||
|
use Monolog\Formatter\FormatterInterface;
|
||||||
|
use Monolog\Formatter\ElasticsearchFormatter;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use Elasticsearch\Common\Exceptions\RuntimeException as ElasticsearchRuntimeException;
|
||||||
|
use Elasticsearch\Client;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Elasticsearch handler
|
* Elasticsearch handler
|
||||||
@@ -148,12 +148,44 @@ class ElasticsearchHandler extends AbstractProcessingHandler
|
|||||||
$responses = $this->client->bulk($params);
|
$responses = $this->client->bulk($params);
|
||||||
|
|
||||||
if ($responses['errors'] === true) {
|
if ($responses['errors'] === true) {
|
||||||
throw new ElasticsearchRuntimeException('Elasticsearch returned error for one of the records');
|
throw $this->createExceptionFromResponses($responses);
|
||||||
}
|
}
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
if (! $this->options['ignore_error']) {
|
if (! $this->options['ignore_error']) {
|
||||||
throw new RuntimeException('Error sending messages to Elasticsearch', 0, $e);
|
throw new RuntimeException('Error sending messages to Elasticsearch', 0, $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates elasticsearch exception from responses array
|
||||||
|
*
|
||||||
|
* Only the first error is converted into an exception.
|
||||||
|
*
|
||||||
|
* @param array $responses returned by $this->client->bulk()
|
||||||
|
*/
|
||||||
|
protected function createExceptionFromResponses(array $responses): ElasticsearchRuntimeException
|
||||||
|
{
|
||||||
|
foreach ($responses['items'] ?? [] as $item) {
|
||||||
|
if (isset($item['index']['error'])) {
|
||||||
|
return $this->createExceptionFromError($item['index']['error']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ElasticsearchRuntimeException('Elasticsearch failed to index one or more records.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates elasticsearch exception from error array
|
||||||
|
*
|
||||||
|
* @param array $error
|
||||||
|
*/
|
||||||
|
protected function createExceptionFromError(array $error): ElasticsearchRuntimeException
|
||||||
|
{
|
||||||
|
$previous = isset($error['caused_by']) ? $this->createExceptionFromError($error['caused_by']) : null;
|
||||||
|
|
||||||
|
return new ElasticsearchRuntimeException($error['type'] . ': ' . $error['reason'], 0, $previous);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user