Skip to content

Commit 78daf5a

Browse files
authored
Merge pull request #224 from zenoleg/fix/db-exception-in-body
fix(statement): Avoid Marking Responses as Failed When Data Contains DB::Exception Text as a Value
2 parents 3bc7471 + d882cf2 commit 78daf5a

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/Statement.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,18 @@ private function parseErrorClickHouse(string $body)
152152
return false;
153153
}
154154

155-
private function hasErrorClickhouse(string $body): bool {
155+
private function hasErrorClickhouse(string $body, string $contentType): bool {
156+
if (false === stripos($contentType, 'application/json')) {
157+
return preg_match(self::CLICKHOUSE_ERROR_REGEX, $body) === 1;
158+
}
156159

157-
return preg_match(self::CLICKHOUSE_ERROR_REGEX, $body) === 1;
160+
try {
161+
json_decode($body, true, 512, JSON_THROW_ON_ERROR);
162+
} catch (\JsonException $e) {
163+
return true;
164+
}
165+
166+
return false;
158167
}
159168

160169
/**
@@ -214,7 +223,7 @@ public function isError(): bool
214223
return true;
215224
}
216225

217-
if ($this->hasErrorClickhouse($this->response()->body())) {
226+
if ($this->hasErrorClickhouse($this->response()->body(), $this->response()->content_type())) {
218227
return true;
219228
}
220229

tests/StatementTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,26 @@ public function testIsErrorWithOkStatusCode()
5050
$this->assertTrue($result->isError());
5151
}
5252

53+
/**
54+
* @link https://github.com/smi2/phpClickHouse/issues/223
55+
* @see src/Statement.php:14
56+
*
57+
* The response data may legitimately contain text that matches the
58+
* CLICKHOUSE_ERROR_REGEX pattern. This is particularly common when querying
59+
* system tables like system.mutations, where error messages are stored as data
60+
*/
61+
public function testIsNotErrorWhenJsonBodyContainsDbExceptionMessage()
62+
{
63+
$result = $this->client->select(
64+
"SELECT
65+
'mutation_123456' AS mutation_id,
66+
'Code: 243. DB::Exception: Cannot reserve 61.64 GiB, not enough space. (NOT_ENOUGH_SPACE) (version 24.3.2.23 (official build))' AS latest_fail_reason"
67+
);
68+
69+
$this->assertEquals(200, $result->getRequest()->response()->http_code());
70+
$this->assertFalse($result->isError());
71+
}
72+
5373
/**
5474
* @dataProvider dataProvider
5575
*/

0 commit comments

Comments
 (0)