Skip to content

Commit

Permalink
Merge pull request #224 from zenoleg/fix/db-exception-in-body
Browse files Browse the repository at this point in the history
fix(statement): Avoid Marking Responses as Failed When Data Contains DB::Exception Text as a Value
  • Loading branch information
isublimity authored Jan 6, 2025
2 parents 3bc7471 + d882cf2 commit 78daf5a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,18 @@ private function parseErrorClickHouse(string $body)
return false;
}

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

return preg_match(self::CLICKHOUSE_ERROR_REGEX, $body) === 1;
try {
json_decode($body, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
return true;
}

return false;
}

/**
Expand Down Expand Up @@ -214,7 +223,7 @@ public function isError(): bool
return true;
}

if ($this->hasErrorClickhouse($this->response()->body())) {
if ($this->hasErrorClickhouse($this->response()->body(), $this->response()->content_type())) {
return true;
}

Expand Down
20 changes: 20 additions & 0 deletions tests/StatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ public function testIsErrorWithOkStatusCode()
$this->assertTrue($result->isError());
}

/**
* @link https://github.com/smi2/phpClickHouse/issues/223
* @see src/Statement.php:14
*
* The response data may legitimately contain text that matches the
* CLICKHOUSE_ERROR_REGEX pattern. This is particularly common when querying
* system tables like system.mutations, where error messages are stored as data
*/
public function testIsNotErrorWhenJsonBodyContainsDbExceptionMessage()
{
$result = $this->client->select(
"SELECT
'mutation_123456' AS mutation_id,
'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"
);

$this->assertEquals(200, $result->getRequest()->response()->http_code());
$this->assertFalse($result->isError());
}

/**
* @dataProvider dataProvider
*/
Expand Down

0 comments on commit 78daf5a

Please sign in to comment.