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

ClickHouseDB\Statement::parseErrorClickHouse update pattern for Clickhouse version 22.8.3.13 (official build) #180

Merged
merged 1 commit into from
Dec 20, 2022
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
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
];
}
}