-
-
Notifications
You must be signed in to change notification settings - Fork 48
Fix #1169: Improve ConvertException error matching (#1170)
#1170
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
base: master
Are you sure you want to change the base?
Changes from all commits
ef71031
1f8ae52
a4831b7
48aa8b9
c089b4e
2c06a9d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Db\Exception; | ||
|
|
||
| /** | ||
| * Represents an exception caused by a database connection failure. | ||
| */ | ||
| final class ConnectionException extends Exception {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,11 @@ | |
| namespace Yiisoft\Db\Tests\Db\Exception; | ||
|
|
||
| use Exception; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Yiisoft\Db\Exception\ConnectionException; | ||
| use Yiisoft\Db\Exception\ConvertException; | ||
| use Yiisoft\Db\Exception\IntegrityException; | ||
|
|
||
| use const PHP_EOL; | ||
|
|
||
|
|
@@ -17,6 +20,14 @@ | |
| */ | ||
| final class ConvertExceptionTest extends TestCase | ||
| { | ||
| #[DataProvider('integrityExceptionMessages')] | ||
| public function testIntegrityException(string $message): void | ||
| { | ||
| $exception = (new ConvertException(new Exception($message), 'INSERT INTO test'))->run(); | ||
|
|
||
| $this->assertInstanceOf(IntegrityException::class, $exception); | ||
| } | ||
|
|
||
| public function testRun(): void | ||
| { | ||
| $e = new Exception('test'); | ||
|
|
@@ -27,4 +38,57 @@ public function testRun(): void | |
| $this->assertSame($e, $exception->getPrevious()); | ||
| $this->assertSame('test' . PHP_EOL . 'The SQL being executed was: ' . $rawSql, $exception->getMessage()); | ||
| } | ||
|
|
||
| #[DataProvider('connectionExceptionMessages')] | ||
| public function testConnectionException(string $message): void | ||
| { | ||
| $exception = (new ConvertException(new Exception($message), 'SELECT 1'))->run(); | ||
|
|
||
| $this->assertInstanceOf(ConnectionException::class, $exception); | ||
| } | ||
|
|
||
| #[DataProvider('generalExceptionMessages')] | ||
| public function testGeneralException(string $message): void | ||
| { | ||
| $exception = (new ConvertException(new Exception($message), 'SELECT 1'))->run(); | ||
|
|
||
| $this->assertNotInstanceOf(IntegrityException::class, $exception); | ||
| $this->assertNotInstanceOf(ConnectionException::class, $exception); | ||
| } | ||
|
|
||
| public static function connectionExceptionMessages(): array | ||
| { | ||
| return [ | ||
| 'connection exception' => ['SQLSTATE[08000]: Connection exception'], | ||
| 'sqlclient unable to establish connection' => ['SQLSTATE[08001]: SQL-client unable to establish SQL-connection'], | ||
| 'connection does not exist' => ['SQLSTATE[08003]: Connection does not exist'], | ||
| 'sqlserver rejected connection' => ['SQLSTATE[08004]: SQL server rejected establishment of SQL-connection'], | ||
| 'connection failure' => ['SQLSTATE[08006]: Connection failure: 7 no connection to the server'], | ||
| ]; | ||
| } | ||
|
|
||
| public static function generalExceptionMessages(): array | ||
| { | ||
| return [ | ||
| 'general error' => ['SQLSTATE[HY000]: General error: 7 no connection to the server'], | ||
| ]; | ||
| } | ||
|
|
||
| public static function integrityExceptionMessages(): array | ||
| { | ||
| return [ | ||
| 'mysql server has gone away' => ['SQLSTATE[HY000]: General error: 2006 MySQL server has gone away'], | ||
| 'mysql server disconnected inactive client' => [ | ||
| 'SQLSTATE[HY000]: General error: 4031 The client was disconnected by the server because of inactivity.', | ||
| ], | ||
|
Comment on lines
+80
to
+83
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These also should be converted to
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, these cases should become
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It requires to add changes in these packages too. The branch name should be the same |
||
| 'sqlstate class 23' => ['SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed'], | ||
| 'oracle unique constraint' => ['ORA-00001: unique constraint (SYS.PK_ID) violated'], | ||
| 'oracle cannot insert null' => ['ORA-01400: cannot insert NULL into ("SYS"."PROFILE"."DESCRIPTION")'], | ||
| 'oracle cannot update null' => ['ORA-01407: cannot update ("SYS"."PROFILE"."DESCRIPTION") to NULL'], | ||
| 'oracle check constraint' => ['ORA-02290: check constraint (SYS.CK_PROFILE_DESCRIPTION) violated'], | ||
| 'oracle parent key not found' => ['ORA-02291: integrity constraint (SYS.FK_PROFILE_CUSTOMER) violated - parent key not found'], | ||
| 'oracle child record found' => ['ORA-02292: integrity constraint (SYS.FK_PROFILE_CUSTOMER) violated - child record found'], | ||
| 'oracle table does not exist for migration compatibility' => ['ORA-00942: table or view does not exist'], | ||
| ]; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why only MYSQL? It could be PostgreSQL also