Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(statement): Avoid Marking Responses as Failed When Data Contains DB::Exception Text as a Value #224

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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