diff --git a/CHANGELOG.md b/CHANGELOG.md index 734583115..82606cc73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Core Admin Queries - Endpoint::getServerUri - Endpoint::getCoreBaseUri +- Expression::indent +- Set erroneous expression on StreamException ### Changed diff --git a/src/Exception/StreamException.php b/src/Exception/StreamException.php index 94da44ed4..64a57bc7b 100644 --- a/src/Exception/StreamException.php +++ b/src/Exception/StreamException.php @@ -7,4 +7,28 @@ */ class StreamException extends \UnexpectedValueException implements ExceptionInterface { + /** + * @var string the streaming expression + */ + protected $expression = ''; + + /** + * Set the streaming expression that caused the exception. + * + * @param string $expression + */ + public function setExpression(string $expression) + { + $this->expression = $expression; + } + + /** + * Get the streaming expression that caused the exception. + * + * @return string + */ + public function getExpression() + { + return $this->expression; + } } diff --git a/src/QueryType/Stream/Expression.php b/src/QueryType/Stream/Expression.php index 9905eae71..9364e5fe6 100644 --- a/src/QueryType/Stream/Expression.php +++ b/src/QueryType/Stream/Expression.php @@ -30,4 +30,38 @@ public function __call(string $name, array $arguments) return '' !== $value; })).')'; } + + /** + * Format and indent a streaming expression. + * + * @param string $expression + * + * @return string + */ + public static function indent(string $expression) + { + $current_indentation = 0; + $indentation_step = 2; + $indented_expression = ''; + for ($c = 0; $c < strlen($expression); ++$c) { + if ('(' === $expression[$c]) { + $indented_expression .= $expression[$c].PHP_EOL; + $current_indentation += $indentation_step; + $indented_expression .= str_pad('', $current_indentation); + } elseif (')' === $expression[$c]) { + $current_indentation -= $indentation_step; + $indented_expression .= PHP_EOL; + $indented_expression .= str_pad('', $current_indentation).$expression[$c]; + } elseif (',' === $expression[$c]) { + $indented_expression .= $expression[$c].PHP_EOL.str_pad('', $current_indentation); + // swallow space if any + if (' ' === @$expression[$c + 1]) { + ++$c; + } + } else { + $indented_expression .= $expression[$c]; + } + } + return $indented_expression; + } } diff --git a/src/QueryType/Stream/ResponseParser.php b/src/QueryType/Stream/ResponseParser.php index 401828857..63ba572fa 100644 --- a/src/QueryType/Stream/ResponseParser.php +++ b/src/QueryType/Stream/ResponseParser.php @@ -44,7 +44,9 @@ public function parse($result) $fields = (array) $doc; if (isset($fields['EXCEPTION'])) { // Use Solr's exception as message. - throw new StreamException($fields['EXCEPTION']); + $e = new StreamException($fields['EXCEPTION']); + $e->setExpression($query->getExpression()); + throw $e; } if (isset($fields['EOF'])) { // End of stream. @@ -53,12 +55,16 @@ public function parse($result) $documents[] = new $documentClass($fields); } if (!isset($fields['EOF'])) { - throw new StreamException('Streaming expression returned an incomplete result-set.'); + $e = new StreamException('Streaming expression returned an incomplete result-set.'); + $e->setExpression($query->getExpression()); + throw $e; } $data['responseHeader']['QTime'] = $fields['RESPONSE_TIME']; $data['responseHeader']['status'] = 0; } else { - throw new StreamException('Streaming expression did not return a result-set.'); + $e = new StreamException('Streaming expression did not return a result-set.'); + $e->setExpression($query->getExpression()); + throw $e; } return $this->addHeaderInfo(