Skip to content

Commit

Permalink
Merge pull request #180 from Vitaly-B/statement_parse_error_clickhouse
Browse files Browse the repository at this point in the history
ClickHouseDB\Statement::parseErrorClickHouse update pattern for Clickhouse version 22.8.3.13 (official build)
  • Loading branch information
isublimity committed Dec 20, 2022
2 parents 2416a68 + c04c1f3 commit 7e7e83d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,12 @@ private function parseErrorClickHouse($body)
$body = trim($body);
$mathes = [];

// Code: 115, e.displayText() = DB::Exception: Unknown setting readonly[0], e.what() = DB::Exception
// Code: 192, e.displayText() = DB::Exception: Unknown user x, e.what() = DB::Exception
// Code: 60, e.displayText() = DB::Exception: Table default.ZZZZZ doesn't exist., e.what() = DB::Exception
// Code: 115. DB::Exception: Unknown setting readonly[0], e.what() = DB::Exception
// Code: 192. DB::Exception: Unknown user x, e.what() = DB::Exception
// Code: 60. DB::Exception: Table default.ZZZZZ doesn't exist., e.what() = DB::Exception
// 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))

if (preg_match("%Code: (\d+),\se\.displayText\(\) \=\s*DB\:\:Exception\s*:\s*(.*)(?:\,\s*e\.what|\(version).*%ius", $body, $mathes)) {
if (preg_match("%Code:\s(\d+).\s*DB\:\:Exception\s*:\s*(.*)(?:\,\s*e\.what|\(version).*%ius", $body, $mathes)) {
return ['code' => $mathes[1], 'message' => $mathes[2]];
}

Expand Down
76 changes: 76 additions & 0 deletions tests/StatementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace ClickHouseDB\Tests;

use ClickHouseDB\Exception\DatabaseException;
use ClickHouseDB\Statement;
use ClickHouseDB\Transport\CurlerRequest;
use ClickHouseDB\Transport\CurlerResponse;
use Generator;
use PHPUnit\Framework\TestCase;

/**
* Class StatementTest
* @group StatementTest
*/
final class StatementTest extends TestCase
{
/**
* @dataProvider dataProvider
*/
public function testParseErrorClickHouse(
string $errorMessage,
string $exceptionMessage,
int $exceptionCode
): void {
$requestMock = $this->createMock(CurlerRequest::class);
$responseMock = $this->createMock(CurlerResponse::class);

$responseMock->expects($this->any())->method('body')->will($this->returnValue($errorMessage));
$responseMock->expects($this->any())->method('error_no')->will($this->returnValue(0));
$responseMock->expects($this->any())->method('error')->will($this->returnValue(false));

$requestMock->expects($this->any())->method('response')->will($this->returnValue($responseMock));

$statement = new Statement($requestMock);
$this->assertInstanceOf(Statement::class, $statement);

$this->expectException(DatabaseException::class);
$this->expectDeprecationMessage($exceptionMessage);
$this->expectExceptionCode($exceptionCode);

$statement->error();
}

/**
* @return Generator
*/
public function dataProvider(): Generator
{
yield 'Unknown setting readonly' => [
'Code: 115. DB::Exception: Unknown setting readonly[0], e.what() = DB::Exception',
'Unknown setting readonly[0]',
115,
];

yield 'Unknown user x' => [
'Code: 192. DB::Exception: Unknown user x, e.what() = DB::Exception',
'Unknown user x',
192,
];

yield 'Table default.ZZZZZ doesn\'t exist.' => [
'Code: 60. DB::Exception: Table default.ZZZZZ doesn\'t exist., e.what() = DB::Exception',
'Table default.ZZZZZ doesn\'t exist.',
60,
];

yield 'Authentication failed' => [
'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))',
'test_username: Authentication failed: password is incorrect or there is no user with such name. (AUTHENTICATION_FAILED)',
516
];
}
}

0 comments on commit 7e7e83d

Please sign in to comment.