summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/lib/Elastica/Exception/ElasticsearchException.php
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2016-05-01 15:30:47 -0400
committerLuke Shumaker <lukeshu@sbcglobal.net>2016-05-01 15:30:47 -0400
commit7e85254903c7c0cb49e381f16b18441ea7b058cc (patch)
treeb22328fcf4c8408fc25a7acb73d1cb1089cd82ac /vendor/ruflin/elastica/lib/Elastica/Exception/ElasticsearchException.php
parent1de335ad3f395ca6861085393ba366a9e3fb4a0d (diff)
parent1a365e77dfb8825136626202b1df462731b42060 (diff)
Merge commit '1a365e'
Diffstat (limited to 'vendor/ruflin/elastica/lib/Elastica/Exception/ElasticsearchException.php')
-rw-r--r--vendor/ruflin/elastica/lib/Elastica/Exception/ElasticsearchException.php99
1 files changed, 99 insertions, 0 deletions
diff --git a/vendor/ruflin/elastica/lib/Elastica/Exception/ElasticsearchException.php b/vendor/ruflin/elastica/lib/Elastica/Exception/ElasticsearchException.php
new file mode 100644
index 00000000..0f7509f9
--- /dev/null
+++ b/vendor/ruflin/elastica/lib/Elastica/Exception/ElasticsearchException.php
@@ -0,0 +1,99 @@
+<?php
+
+namespace Elastica\Exception;
+
+/**
+ * Elasticsearch exception
+ *
+ * @category Xodoa
+ * @package Elastica
+ * @author Ian Babrou <ibobrik@gmail.com>
+ */
+class ElasticsearchException extends \Exception
+{
+
+ const REMOTE_TRANSPORT_EXCEPTION = 'RemoteTransportException';
+
+ /**
+ * Elasticsearch exception name
+ *
+ * @var string|null
+ */
+ private $_exception;
+
+ /**
+ * Whether exception was local to server node or remote
+ *
+ * @var bool
+ */
+ private $_isRemote = false;
+
+ /**
+ * Constructs elasticsearch exception
+ *
+ * @param int $code Error code
+ * @param string $error Error message from elasticsearch
+ */
+ public function __construct($code, $error)
+ {
+ $this->_parseError($error);
+ parent::__construct($error, $code);
+ }
+
+ /**
+ * Parse error message from elasticsearch
+ *
+ * @param string $error Error message
+ */
+ protected function _parseError($error)
+ {
+ $errors = explode(']; nested: ', $error);
+
+ if (count($errors) == 1) {
+ $this->_exception = $this->_extractException($errors[0]);
+ } else {
+ if ($this->_extractException($errors[0]) == self::REMOTE_TRANSPORT_EXCEPTION) {
+ $this->_isRemote = true;
+ $this->_exception = $this->_extractException($errors[1]);
+ } else {
+ $this->_exception = $this->_extractException($errors[0]);
+ }
+ }
+ }
+
+ /**
+ * Extract exception name from error response
+ *
+ * @param string $error
+ * @return null|string
+ */
+ protected function _extractException($error)
+ {
+ if (preg_match('/^(\w+)\[.*\]/', $error, $matches)) {
+ return $matches[1];
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Returns elasticsearch exception name
+ *
+ * @return string|null
+ */
+ public function getExceptionName()
+ {
+ return $this->_exception;
+ }
+
+ /**
+ * Returns whether exception was local to server node or remote
+ *
+ * @return bool
+ */
+ public function isRemoteTransportException()
+ {
+ return $this->_isRemote;
+ }
+
+}