From 53c123906305d689a11fcfa17252af1a45956642 Mon Sep 17 00:00:00 2001 From: Markus Kalkbrenner Date: Wed, 31 Oct 2018 14:03:48 +0100 Subject: [PATCH] =?UTF-8?q?Set=20the=20erroneous=20expression=20on=20a=20S?= =?UTF-8?q?treamException.=20Added=20Expression::=E2=80=A6=20(#635)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Set the erroneous expression on a StreamException. Added Expression::indent to get readable streaming expressions. --- CHANGELOG.md | 2 ++ src/Exception/StreamException.php | 24 +++++++++++++++++ src/QueryType/Stream/Expression.php | 34 +++++++++++++++++++++++++ src/QueryType/Stream/ResponseParser.php | 12 ++++++--- 4 files changed, 69 insertions(+), 3 deletions(-) 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(