Skip to content

Commit a629f30

Browse files
authored
Merge pull request #205 from anathex/master
Fixed silent error occurrence on 200 http status code (ExceptionWhileProcessing query error)
2 parents 975b6d6 + 1595cd0 commit a629f30

File tree

2 files changed

+60
-8
lines changed

2 files changed

+60
-8
lines changed

src/Statement.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
class Statement implements \Iterator
1313
{
14+
private const CLICKHOUSE_ERROR_REGEX = "%Code:\s(\d+)\.\s*DB::Exception\s*:\s*(.*)(?:,\s*e\.what|\(version).*%ius";
15+
1416
/**
1517
* @var string|mixed
1618
*/
@@ -133,23 +135,28 @@ public function sql()
133135
* @param string $body
134136
* @return array|bool
135137
*/
136-
private function parseErrorClickHouse($body)
138+
private function parseErrorClickHouse(string $body)
137139
{
138140
$body = trim($body);
139-
$mathes = [];
141+
$matches = [];
140142

141143
// Code: 115. DB::Exception: Unknown setting readonly[0], e.what() = DB::Exception
142144
// Code: 192. DB::Exception: Unknown user x, e.what() = DB::Exception
143145
// Code: 60. DB::Exception: Table default.ZZZZZ doesn't exist., e.what() = DB::Exception
144146
// Code: 516. DB::Exception: test_username: Authentication failed: password is incorrect or there is no user with such name. (AUTHENTICATION_FAILED) (version 22.8.3.13 (official build))
145147

146-
if (preg_match("%Code:\s(\d+).\s*DB\:\:Exception\s*:\s*(.*)(?:\,\s*e\.what|\(version).*%ius", $body, $mathes)) {
147-
return ['code' => $mathes[1], 'message' => $mathes[2]];
148+
if (preg_match(self::CLICKHOUSE_ERROR_REGEX, $body, $matches)) {
149+
return ['code' => $matches[1], 'message' => $matches[2]];
148150
}
149151

150152
return false;
151153
}
152154

155+
private function hasErrorClickhouse(string $body): bool {
156+
157+
return preg_match(self::CLICKHOUSE_ERROR_REGEX, $body) === 1;
158+
}
159+
153160
/**
154161
* @return bool
155162
* @throws Exception\TransportException
@@ -197,12 +204,24 @@ public function error()
197204
* @return bool
198205
* @throws Exception\TransportException
199206
*/
200-
public function isError()
207+
public function isError(): bool
201208
{
202-
return ($this->response()->http_code() !== 200 || $this->response()->error_no());
209+
if ($this->response()->http_code() !== 200) {
210+
return true;
211+
}
212+
213+
if ($this->response()->error_no()) {
214+
return true;
215+
}
216+
217+
if ($this->hasErrorClickhouse($this->response()->body())) {
218+
return true;
219+
}
220+
221+
return false;
203222
}
204223

205-
private function check() : bool
224+
private function check(): bool
206225
{
207226
if (!$this->_request->isResponseExists()) {
208227
throw QueryException::noResponse();

tests/StatementTest.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,39 @@
1717
*/
1818
final class StatementTest extends TestCase
1919
{
20+
use WithClient;
21+
22+
public function testIsError()
23+
{
24+
$result = $this->client->select(
25+
'SELECT throwIf(1=1, \'Raised error\');'
26+
);
27+
28+
$this->assertGreaterThanOrEqual(500, $result->getRequest()->response()->http_code());
29+
$this->assertTrue($result->isError());
30+
}
31+
32+
/**
33+
* @link https://github.com/smi2/phpClickHouse/issues/144
34+
* @link https://clickhouse.com/docs/en/interfaces/http#http_response_codes_caveats
35+
*
36+
* During execution of query it is possible to get ExceptionWhileProcessing in Clickhouse
37+
* In that case HTTP status code of Clickhouse interface would be 200
38+
* and it is kind of "expected" behaviour of CH
39+
*/
40+
public function testIsErrorWithOkStatusCode()
41+
{
42+
// value of "number" in query must be greater than 100 thousand
43+
// for part of CH response to be flushed to client with 200 status code
44+
// and further ExceptionWhileProcessing occurrence
45+
$result = $this->client->select(
46+
'SELECT number, throwIf(number=100100, \'Raised error\') FROM system.numbers;'
47+
);
48+
49+
$this->assertEquals(200, $result->getRequest()->response()->http_code());
50+
$this->assertTrue($result->isError());
51+
}
52+
2053
/**
2154
* @dataProvider dataProvider
2255
*/
@@ -38,7 +71,7 @@ public function testParseErrorClickHouse(
3871
$this->assertInstanceOf(Statement::class, $statement);
3972

4073
$this->expectException(DatabaseException::class);
41-
$this->expectDeprecationMessage($exceptionMessage);
74+
$this->expectExceptionMessage($exceptionMessage);
4275
$this->expectExceptionCode($exceptionCode);
4376

4477
$statement->error();

0 commit comments

Comments
 (0)