diff --git a/src/Statement.php b/src/Statement.php index bf3e753..7639c61 100644 --- a/src/Statement.php +++ b/src/Statement.php @@ -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]]; } diff --git a/tests/StatementTest.php b/tests/StatementTest.php new file mode 100644 index 0000000..f4117a4 --- /dev/null +++ b/tests/StatementTest.php @@ -0,0 +1,76 @@ +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 + ]; + } +}