Skip to content

Commit c04c1f3

Browse files
author
Vitaly Belikov
committed
ClickHouseDB\Statement::parseErrorClickHouse update pattern for Clickhouse version 22.8.3.13 (official build)
1 parent 3c01fa0 commit c04c1f3

File tree

2 files changed

+81
-4
lines changed

2 files changed

+81
-4
lines changed

src/Statement.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,12 @@ private function parseErrorClickHouse($body)
138138
$body = trim($body);
139139
$mathes = [];
140140

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

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

tests/StatementTest.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ClickHouseDB\Tests;
6+
7+
use ClickHouseDB\Exception\DatabaseException;
8+
use ClickHouseDB\Statement;
9+
use ClickHouseDB\Transport\CurlerRequest;
10+
use ClickHouseDB\Transport\CurlerResponse;
11+
use Generator;
12+
use PHPUnit\Framework\TestCase;
13+
14+
/**
15+
* Class StatementTest
16+
* @group StatementTest
17+
*/
18+
final class StatementTest extends TestCase
19+
{
20+
/**
21+
* @dataProvider dataProvider
22+
*/
23+
public function testParseErrorClickHouse(
24+
string $errorMessage,
25+
string $exceptionMessage,
26+
int $exceptionCode
27+
): void {
28+
$requestMock = $this->createMock(CurlerRequest::class);
29+
$responseMock = $this->createMock(CurlerResponse::class);
30+
31+
$responseMock->expects($this->any())->method('body')->will($this->returnValue($errorMessage));
32+
$responseMock->expects($this->any())->method('error_no')->will($this->returnValue(0));
33+
$responseMock->expects($this->any())->method('error')->will($this->returnValue(false));
34+
35+
$requestMock->expects($this->any())->method('response')->will($this->returnValue($responseMock));
36+
37+
$statement = new Statement($requestMock);
38+
$this->assertInstanceOf(Statement::class, $statement);
39+
40+
$this->expectException(DatabaseException::class);
41+
$this->expectDeprecationMessage($exceptionMessage);
42+
$this->expectExceptionCode($exceptionCode);
43+
44+
$statement->error();
45+
}
46+
47+
/**
48+
* @return Generator
49+
*/
50+
public function dataProvider(): Generator
51+
{
52+
yield 'Unknown setting readonly' => [
53+
'Code: 115. DB::Exception: Unknown setting readonly[0], e.what() = DB::Exception',
54+
'Unknown setting readonly[0]',
55+
115,
56+
];
57+
58+
yield 'Unknown user x' => [
59+
'Code: 192. DB::Exception: Unknown user x, e.what() = DB::Exception',
60+
'Unknown user x',
61+
192,
62+
];
63+
64+
yield 'Table default.ZZZZZ doesn\'t exist.' => [
65+
'Code: 60. DB::Exception: Table default.ZZZZZ doesn\'t exist., e.what() = DB::Exception',
66+
'Table default.ZZZZZ doesn\'t exist.',
67+
60,
68+
];
69+
70+
yield 'Authentication failed' => [
71+
'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))',
72+
'test_username: Authentication failed: password is incorrect or there is no user with such name. (AUTHENTICATION_FAILED)',
73+
516
74+
];
75+
}
76+
}

0 commit comments

Comments
 (0)