Skip to content

Commit 87b17fa

Browse files
committed
Populate driver exception error information
Preserve native PDO diagnostics and provide MySQL-compatible SQLSTATE, vendor code, and message tuples for emulated errors.
1 parent bacf013 commit 87b17fa

4 files changed

Lines changed: 105 additions & 47 deletions

File tree

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<?php
22

3+
/*
4+
* PDO uses camel case naming, enable non-snake case:
5+
* phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
6+
*/
7+
38
class WP_MySQL_On_SQLite_Exception extends PDOException {
49
/**
510
* The MySQL-on-SQLite driver that originated the exception.
@@ -11,23 +16,44 @@ class WP_MySQL_On_SQLite_Exception extends PDOException {
1116
/**
1217
* Constructor.
1318
*
14-
* @param WP_MySQL_On_SQLite $driver The MySQL-on-SQLite driver that originated the exception.
15-
* @param string $message The exception message.
16-
* @param int|string $code The exception code. In PDO, it can be a string with value of SQLSTATE.
17-
* @param Throwable|null $previous The previous throwable used for the exception chaining.
19+
* @param WP_MySQL_On_SQLite $driver The MySQL-on-SQLite driver that originated the exception.
20+
* @param string $message The exception message.
21+
* @param int|string $code The exception code. In PDO, it can be a string with value of SQLSTATE.
22+
* @param Throwable|null $previous The previous throwable used for the exception chaining.
23+
* @param array|null $error_info PDO-style error information.
1824
*/
1925
public function __construct(
2026
WP_MySQL_On_SQLite $driver,
2127
string $message,
2228
$code = 0,
23-
?Throwable $previous = null
29+
?Throwable $previous = null,
30+
?array $error_info = null
2431
) {
2532
parent::__construct( $message, 0, $previous );
26-
$this->code = $code;
27-
$this->driver = $driver;
33+
$this->code = $code;
34+
$this->driver = $driver;
35+
$this->errorInfo = $error_info ?? $this->create_error_info( $message, $code, $previous );
2836
}
2937

3038
public function get_driver(): WP_MySQL_On_SQLite {
3139
return $this->driver;
3240
}
41+
42+
/**
43+
* Create PDO-style error information from an originating exception or from
44+
* the emulated driver error.
45+
*
46+
* @param string $message The exception message.
47+
* @param int|string $code The exception code.
48+
* @param Throwable|null $previous The previous throwable.
49+
* @return array PDO-style error information.
50+
*/
51+
private function create_error_info( string $message, $code, ?Throwable $previous ): array {
52+
if ( $previous instanceof PDOException && is_array( $previous->errorInfo ) ) {
53+
return $previous->errorInfo;
54+
}
55+
56+
$sqlstate = is_string( $code ) && 5 === strlen( $code ) ? $code : 'HY000';
57+
return array( $sqlstate, 1105, $message );
58+
}
3359
}

‎packages/mysql-on-sqlite/src/sqlite/class-wp-mysql-on-sqlite.php‎

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7519,17 +7519,19 @@ private function create_result_statement_from_data( array $columns, array $rows
75197519
/**
75207520
* Create a new MySQL-on-SQLite driver exception.
75217521
*
7522-
* @param string $message The exception message.
7523-
* @param int|string $code The exception code. For PDO errors, a string representing SQLSTATE.
7524-
* @param Throwable|null $previous The previous exception.
7522+
* @param string $message The exception message.
7523+
* @param int|string $code The exception code. For PDO errors, a string representing SQLSTATE.
7524+
* @param Throwable|null $previous The previous exception.
7525+
* @param array|null $error_info PDO-style error information.
75257526
* @return WP_MySQL_On_SQLite_Exception
75267527
*/
75277528
private function new_driver_exception(
75287529
string $message,
75297530
$code = 0,
7530-
?Throwable $previous = null
7531+
?Throwable $previous = null,
7532+
?array $error_info = null
75317533
): WP_MySQL_On_SQLite_Exception {
7532-
return new WP_MySQL_On_SQLite_Exception( $this, $message, $code, $previous );
7534+
return new WP_MySQL_On_SQLite_Exception( $this, $message, $code, $previous, $error_info );
75337535
}
75347536

75357537
/**
@@ -7601,52 +7603,55 @@ private function new_access_denied_to_information_schema_exception(): WP_MySQL_O
76017603
private function convert_information_schema_exception( WP_SQLite_Information_Schema_Exception $e ): Throwable {
76027604
switch ( $e->get_type() ) {
76037605
case WP_SQLite_Information_Schema_Exception::TYPE_DUPLICATE_TABLE_NAME:
7606+
$driver_message = sprintf( "Table '%s' already exists", $e->get_data()['table_name'] );
76047607
return $this->new_driver_exception(
7605-
sprintf(
7606-
"SQLSTATE[42S01]: Base table or view already exists: 1050 Table '%s' already exists",
7607-
$e->get_data()['table_name']
7608-
),
7609-
'42S01'
7608+
'SQLSTATE[42S01]: Base table or view already exists: 1050 ' . $driver_message,
7609+
'42S01',
7610+
null,
7611+
array( '42S01', 1050, $driver_message )
76107612
);
76117613
case WP_SQLite_Information_Schema_Exception::TYPE_DUPLICATE_COLUMN_NAME:
7614+
$driver_message = sprintf( "Duplicate column name '%s'", $e->get_data()['column_name'] );
76127615
return $this->new_driver_exception(
7613-
sprintf(
7614-
"SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name '%s'",
7615-
$e->get_data()['column_name']
7616-
),
7617-
'42S21'
7616+
'SQLSTATE[42S21]: Column already exists: 1060 ' . $driver_message,
7617+
'42S21',
7618+
null,
7619+
array( '42S21', 1060, $driver_message )
76187620
);
76197621
case WP_SQLite_Information_Schema_Exception::TYPE_DUPLICATE_KEY_NAME:
7622+
$driver_message = sprintf( "Duplicate key name '%s'", $e->get_data()['key_name'] );
76207623
return $this->new_driver_exception(
7621-
sprintf(
7622-
"SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name '%s'",
7623-
$e->get_data()['key_name']
7624-
),
7625-
'42S21'
7624+
'SQLSTATE[42000]: Syntax error or access violation: 1061 ' . $driver_message,
7625+
'42000',
7626+
null,
7627+
array( '42000', 1061, $driver_message )
76267628
);
76277629
case WP_SQLite_Information_Schema_Exception::TYPE_KEY_COLUMN_NOT_FOUND:
7630+
$driver_message = sprintf( "Key column '%s' doesn't exist in table", $e->get_data()['column_name'] );
76287631
return $this->new_driver_exception(
7629-
sprintf(
7630-
"SQLSTATE[42000]: Syntax error or access violation: 1072 Key column '%s' doesn't exist in table",
7631-
$e->get_data()['column_name']
7632-
),
7633-
'42000'
7632+
'SQLSTATE[42000]: Syntax error or access violation: 1072 ' . $driver_message,
7633+
'42000',
7634+
null,
7635+
array( '42000', 1072, $driver_message )
76347636
);
76357637
case WP_SQLite_Information_Schema_Exception::TYPE_CONSTRAINT_DOES_NOT_EXIST:
7638+
$driver_message = sprintf( "Constraint '%s' does not exist.", $e->get_data()['name'] );
76367639
return $this->new_driver_exception(
7637-
sprintf(
7638-
"SQLSTATE[HY000]: General error: 3940 Constraint '%s' does not exist.",
7639-
$e->get_data()['name']
7640-
),
7641-
'HY000'
7640+
'SQLSTATE[HY000]: General error: 3940 ' . $driver_message,
7641+
'HY000',
7642+
null,
7643+
array( 'HY000', 3940, $driver_message )
76427644
);
76437645
case WP_SQLite_Information_Schema_Exception::TYPE_MULTIPLE_CONSTRAINTS_WITH_NAME:
7646+
$driver_message = sprintf(
7647+
"Table has multiple constraints with the name '%s'. Please use constraint specific 'DROP' clause.",
7648+
$e->get_data()['name']
7649+
);
76447650
return $this->new_driver_exception(
7645-
sprintf(
7646-
"SQLSTATE[HY000]: General error: 3939 Table has multiple constraints with the name '%s'. Please use constraint specific 'DROP' clause.",
7647-
$e->get_data()['name']
7648-
),
7649-
'HY000'
7651+
'SQLSTATE[HY000]: General error: 3939 ' . $driver_message,
7652+
'HY000',
7653+
null,
7654+
array( 'HY000', 3939, $driver_message )
76507655
);
76517656
default:
76527657
return $e;

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_PDO_API_Tests.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,33 @@ public function test_driver_exception_exposes_originating_driver(): void {
3838
$exception = new WP_MySQL_On_SQLite_Exception( $this->driver, 'Test error.' );
3939

4040
$this->assertSame( $this->driver, $exception->get_driver() );
41+
$this->assertSame( array( 'HY000', 1105, 'Test error.' ), $exception->errorInfo );
42+
}
43+
44+
public function test_driver_exception_preserves_pdo_error_information(): void {
45+
try {
46+
$this->driver->query( 'SELECT * FROM missing_table' );
47+
$this->fail( 'Expected query() to throw an exception.' );
48+
} catch ( WP_MySQL_On_SQLite_Exception $exception ) {
49+
$this->assertSame( 'HY000', $exception->errorInfo[0] );
50+
$this->assertSame( 1, $exception->errorInfo[1] );
51+
$this->assertSame( 'no such table: missing_table', $exception->errorInfo[2] );
52+
}
53+
}
54+
55+
public function test_emulated_driver_exception_exposes_mysql_error_information(): void {
56+
$this->driver->query( 'CREATE TABLE t (id INT)' );
57+
58+
try {
59+
$this->driver->query( 'CREATE TABLE t (id INT)' );
60+
$this->fail( 'Expected query() to throw an exception.' );
61+
} catch ( WP_MySQL_On_SQLite_Exception $exception ) {
62+
$this->assertSame( '42S01', $exception->getCode() );
63+
$this->assertSame(
64+
array( '42S01', 1050, "Table 't' already exists" ),
65+
$exception->errorInfo
66+
);
67+
}
4168
}
4269

4370
public function test_exposes_underlying_sqlite_pdo(): void {

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6284,7 +6284,7 @@ public function testCreateTableDuplicateKeyName(): void {
62846284

62856285
$this->assertInstanceOf( WP_MySQL_On_SQLite_Exception::class, $exception );
62866286
$this->assertSame( "SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name 'idx'", $exception->getMessage() );
6287-
$this->assertSame( '42S21', $exception->getCode() );
6287+
$this->assertSame( '42000', $exception->getCode() );
62886288
}
62896289

62906290
public function testCreateTableDuplicateKeyNameWithUnique(): void {
@@ -6297,7 +6297,7 @@ public function testCreateTableDuplicateKeyNameWithUnique(): void {
62976297

62986298
$this->assertInstanceOf( WP_MySQL_On_SQLite_Exception::class, $exception );
62996299
$this->assertSame( "SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name 'idx'", $exception->getMessage() );
6300-
$this->assertSame( '42S21', $exception->getCode() );
6300+
$this->assertSame( '42000', $exception->getCode() );
63016301
}
63026302

63036303
public function testCreateTableDuplicateKeyNameWithPrimaryKey(): void {
@@ -6344,7 +6344,7 @@ public function testAlterTableDuplicateKeyName(): void {
63446344

63456345
$this->assertInstanceOf( WP_MySQL_On_SQLite_Exception::class, $exception );
63466346
$this->assertSame( "SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name 'idx'", $exception->getMessage() );
6347-
$this->assertSame( '42S21', $exception->getCode() );
6347+
$this->assertSame( '42000', $exception->getCode() );
63486348
}
63496349

63506350
public function testAlterTableDuplicateKeyNameWithMultipleOperations(): void {
@@ -6358,7 +6358,7 @@ public function testAlterTableDuplicateKeyNameWithMultipleOperations(): void {
63586358

63596359
$this->assertInstanceOf( WP_MySQL_On_SQLite_Exception::class, $exception );
63606360
$this->assertSame( "SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name 'idx'", $exception->getMessage() );
6361-
$this->assertSame( '42S21', $exception->getCode() );
6361+
$this->assertSame( '42000', $exception->getCode() );
63626362
}
63636363

63646364
public function testAlterTableDuplicateKeyNameWithUnique(): void {
@@ -6372,7 +6372,7 @@ public function testAlterTableDuplicateKeyNameWithUnique(): void {
63726372

63736373
$this->assertInstanceOf( WP_MySQL_On_SQLite_Exception::class, $exception );
63746374
$this->assertSame( "SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name 'idx'", $exception->getMessage() );
6375-
$this->assertSame( '42S21', $exception->getCode() );
6375+
$this->assertSame( '42000', $exception->getCode() );
63766376
}
63776377

63786378
public function testConstraintName(): void {

0 commit comments

Comments
 (0)