Skip to content
Open
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
10 changes: 9 additions & 1 deletion src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,15 @@ private function hasErrorClickhouse(string $body, ?string $contentType): bool {

if (strlen($body) > 4096) {
$tail = substr($body, -4096);
return preg_match(self::CLICKHOUSE_ERROR_REGEX, $tail) === 1;
if (preg_match(self::CLICKHOUSE_ERROR_REGEX, $tail) === 1) {
// Regex also matches if the actual data contains a ClickHouse error
// string. Use json_validate() to confirm the response is truly broken.
if (function_exists('json_validate')) {
return !json_validate($body);
}
return true;
}
return false;
}

try {
Expand Down
36 changes: 36 additions & 0 deletions tests/LargeStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,42 @@ public function testLargeBodyWithErrorAtEnd(): void
$this->assertTrue($statement->isError());
}

/**
* Large body with valid JSON containing ClickHouse error text as data.
*/
public function testLargeJsonWithErrorPatternInDataIsNotError(): void
{
if (!function_exists('json_validate')) {
$this->markTestSkipped('json_validate() not available');
}

$rows = [];
for ($i = 0; $i < 100; $i++) {
$rows[] = '{"id":' . $i . ',"message":"Code: 60. DB::Exception: Table default.xxx doesn\'t exist. (UNKNOWN_TABLE) (version 24.3.2.23 (official build))"}';
}
$body = '{"meta":[{"name":"id","type":"UInt64"},{"name":"message","type":"String"}],'
. '"data":[' . implode(',', $rows) . '],'
. '"rows":100,'
. '"statistics":{"elapsed":0.001,"rows_read":100,"bytes_read":4096}}';

// Ensure body exceeds the 4096-byte threshold
$this->assertGreaterThan(4096, strlen($body));

$responseMock = $this->createMock(CurlerResponse::class);
$responseMock->method('http_code')->willReturn(200);
$responseMock->method('error_no')->willReturn(0);
$responseMock->method('content_type')->willReturn('application/json; charset=UTF-8');
$responseMock->method('body')->willReturn($body);

$requestMock = $this->createMock(CurlerRequest::class);
$requestMock->method('response')->willReturn($responseMock);
$requestMock->method('isResponseExists')->willReturn(true);

$statement = new Statement($requestMock);

$this->assertFalse($statement->isError());
}

/**
* Small body with valid JSON should still be checked for JSON validity.
*/
Expand Down