1<?php 2 3namespace Elastica\Exception; 4 5\trigger_deprecation('ruflin/elastica', '5.2.0', 'The "%s" class is deprecated, use "Elastica\Exception\ResponseException::getResponse()::getFullError()" instead. It will be removed in 8.0.', ElasticsearchException::class); 6 7/** 8 * Elasticsearch exception. 9 * 10 * @author Ian Babrou <ibobrik@gmail.com> 11 * 12 * @deprecated since version 5.2.0 13 */ 14class ElasticsearchException extends \Exception implements ExceptionInterface 15{ 16 public const REMOTE_TRANSPORT_EXCEPTION = 'RemoteTransportException'; 17 18 /** 19 * @var array Error array 20 */ 21 protected $_error = []; 22 23 /** 24 * @var string|null Elasticsearch exception name 25 */ 26 private $_exception; 27 28 /** 29 * @var bool Whether exception was local to server node or remote 30 */ 31 private $_isRemote = false; 32 33 /** 34 * @param int $code Error code 35 * @param string $error Error message from elasticsearch 36 */ 37 public function __construct(int $code, string $error) 38 { 39 $this->_parseError($error); 40 parent::__construct($error, $code); 41 } 42 43 /** 44 * Returns elasticsearch exception name. 45 * 46 * @return string|null 47 */ 48 public function getExceptionName() 49 { 50 return $this->_exception; 51 } 52 53 /** 54 * Returns whether exception was local to server node or remote. 55 */ 56 public function isRemoteTransportException(): bool 57 { 58 return $this->_isRemote; 59 } 60 61 /** 62 * @return array Error array 63 */ 64 public function getError(): array 65 { 66 return $this->_error; 67 } 68 69 /** 70 * Parse error message from elasticsearch. 71 * 72 * @param string $error Error message 73 */ 74 protected function _parseError(string $error): void 75 { 76 $errors = \explode(']; nested: ', $error); 77 78 if (1 === \count($errors)) { 79 $this->_exception = $this->_extractException($errors[0]); 80 } else { 81 if (self::REMOTE_TRANSPORT_EXCEPTION === $this->_extractException($errors[0])) { 82 $this->_isRemote = true; 83 $this->_exception = $this->_extractException($errors[1]); 84 } else { 85 $this->_exception = $this->_extractException($errors[0]); 86 } 87 } 88 } 89 90 /** 91 * Extract exception name from error response. 92 * 93 * @return string|null 94 */ 95 protected function _extractException(string $error) 96 { 97 if (\preg_match('/^(\w+)\[.*\]/', $error, $matches)) { 98 return $matches[1]; 99 } 100 101 return null; 102 } 103} 104