9
9
use Doctrine \DBAL \Cache \CacheException ;
10
10
use Doctrine \DBAL \Cache \CachingResult ;
11
11
use Doctrine \DBAL \Cache \QueryCacheProfile ;
12
+ use Doctrine \DBAL \Driver \API \ExceptionConverter ;
12
13
use Doctrine \DBAL \Driver \Connection as DriverConnection ;
13
14
use Doctrine \DBAL \Driver \Exception as DriverException ;
14
15
use Doctrine \DBAL \Driver \Result as DriverResult ;
26
27
use Traversable ;
27
28
28
29
use function array_key_exists ;
30
+ use function array_map ;
29
31
use function assert ;
32
+ use function bin2hex ;
30
33
use function count ;
31
34
use function implode ;
32
35
use function is_int ;
36
+ use function is_resource ;
33
37
use function is_string ;
38
+ use function json_encode ;
34
39
use function key ;
40
+ use function preg_replace ;
41
+ use function sprintf ;
35
42
36
43
/**
37
44
* A wrapper around a Doctrine\DBAL\Driver\Connection that adds features like
@@ -114,6 +121,9 @@ class Connection implements DriverConnection
114
121
*/
115
122
private $ platform ;
116
123
124
+ /** @var ExceptionConverter|null */
125
+ private $ exceptionConverter ;
126
+
117
127
/**
118
128
* The schema manager.
119
129
*
@@ -284,7 +294,7 @@ public function connect()
284
294
try {
285
295
$ this ->_conn = $ this ->_driver ->connect ($ this ->params );
286
296
} catch (DriverException $ e ) {
287
- throw DBALException:: driverException ( $ this ->_driver , $ e );
297
+ throw $ this ->convertException ( $ e );
288
298
}
289
299
290
300
$ this ->transactionNestingLevel = 0 ;
@@ -468,7 +478,7 @@ public function fetchAssociative(string $query, array $params = [], array $types
468
478
try {
469
479
return $ this ->executeQuery ($ query , $ params , $ types )->fetchAssociative ();
470
480
} catch (DriverException $ e ) {
471
- $ this ->handleExceptionDuringQuery ($ e , $ query , $ params , $ types );
481
+ throw $ this ->convertExceptionDuringQuery ($ e , $ query , $ params , $ types );
472
482
}
473
483
}
474
484
@@ -489,7 +499,7 @@ public function fetchNumeric(string $query, array $params = [], array $types = [
489
499
try {
490
500
return $ this ->executeQuery ($ query , $ params , $ types )->fetchNumeric ();
491
501
} catch (DriverException $ e ) {
492
- $ this ->handleExceptionDuringQuery ($ e , $ query , $ params , $ types );
502
+ throw $ this ->convertExceptionDuringQuery ($ e , $ query , $ params , $ types );
493
503
}
494
504
}
495
505
@@ -510,7 +520,7 @@ public function fetchOne(string $query, array $params = [], array $types = [])
510
520
try {
511
521
return $ this ->executeQuery ($ query , $ params , $ types )->fetchOne ();
512
522
} catch (DriverException $ e ) {
513
- $ this ->handleExceptionDuringQuery ($ e , $ query , $ params , $ types );
523
+ throw $ this ->convertExceptionDuringQuery ($ e , $ query , $ params , $ types );
514
524
}
515
525
}
516
526
@@ -772,7 +782,7 @@ public function fetchAllNumeric(string $query, array $params = [], array $types
772
782
try {
773
783
return $ this ->executeQuery ($ query , $ params , $ types )->fetchAllNumeric ();
774
784
} catch (DriverException $ e ) {
775
- $ this ->handleExceptionDuringQuery ($ e , $ query , $ params , $ types );
785
+ throw $ this ->convertExceptionDuringQuery ($ e , $ query , $ params , $ types );
776
786
}
777
787
}
778
788
@@ -792,7 +802,7 @@ public function fetchAllAssociative(string $query, array $params = [], array $ty
792
802
try {
793
803
return $ this ->executeQuery ($ query , $ params , $ types )->fetchAllAssociative ();
794
804
} catch (DriverException $ e ) {
795
- $ this ->handleExceptionDuringQuery ($ e , $ query , $ params , $ types );
805
+ throw $ this ->convertExceptionDuringQuery ($ e , $ query , $ params , $ types );
796
806
}
797
807
}
798
808
@@ -812,7 +822,7 @@ public function fetchFirstColumn(string $query, array $params = [], array $types
812
822
try {
813
823
return $ this ->executeQuery ($ query , $ params , $ types )->fetchFirstColumn ();
814
824
} catch (DriverException $ e ) {
815
- $ this ->handleExceptionDuringQuery ($ e , $ query , $ params , $ types );
825
+ throw $ this ->convertExceptionDuringQuery ($ e , $ query , $ params , $ types );
816
826
}
817
827
}
818
828
@@ -836,7 +846,7 @@ public function iterateNumeric(string $query, array $params = [], array $types =
836
846
yield $ row ;
837
847
}
838
848
} catch (DriverException $ e ) {
839
- $ this ->handleExceptionDuringQuery ($ e , $ query , $ params , $ types );
849
+ throw $ this ->convertExceptionDuringQuery ($ e , $ query , $ params , $ types );
840
850
}
841
851
}
842
852
@@ -860,7 +870,7 @@ public function iterateAssociative(string $query, array $params = [], array $typ
860
870
yield $ row ;
861
871
}
862
872
} catch (DriverException $ e ) {
863
- $ this ->handleExceptionDuringQuery ($ e , $ query , $ params , $ types );
873
+ throw $ this ->convertExceptionDuringQuery ($ e , $ query , $ params , $ types );
864
874
}
865
875
}
866
876
@@ -884,7 +894,7 @@ public function iterateColumn(string $query, array $params = [], array $types =
884
894
yield $ value ;
885
895
}
886
896
} catch (DriverException $ e ) {
887
- $ this ->handleExceptionDuringQuery ($ e , $ query , $ params , $ types );
897
+ throw $ this ->convertExceptionDuringQuery ($ e , $ query , $ params , $ types );
888
898
}
889
899
}
890
900
@@ -949,7 +959,7 @@ public function executeQuery(
949
959
950
960
return new Result ($ result , $ this );
951
961
} catch (DriverException $ e ) {
952
- $ this ->handleExceptionDuringQuery ($ e , $ query , $ params , $ types );
962
+ throw $ this ->convertExceptionDuringQuery ($ e , $ query , $ params , $ types );
953
963
} finally {
954
964
if ($ logger !== null ) {
955
965
$ logger ->stopQuery ();
@@ -1021,7 +1031,7 @@ public function query(string $sql): DriverResult
1021
1031
try {
1022
1032
return $ connection ->query ($ sql );
1023
1033
} catch (DriverException $ e ) {
1024
- $ this ->handleExceptionDuringQuery ($ e , $ sql );
1034
+ throw $ this ->convertExceptionDuringQuery ($ e , $ sql );
1025
1035
} finally {
1026
1036
if ($ logger !== null ) {
1027
1037
$ logger ->stopQuery ();
@@ -1069,7 +1079,7 @@ public function executeUpdate(string $query, array $params = [], array $types =
1069
1079
1070
1080
return $ connection ->exec ($ query );
1071
1081
} catch (DriverException $ e ) {
1072
- $ this ->handleExceptionDuringQuery ($ e , $ query , $ params , $ types );
1082
+ throw $ this ->convertExceptionDuringQuery ($ e , $ query , $ params , $ types );
1073
1083
} finally {
1074
1084
if ($ logger !== null ) {
1075
1085
$ logger ->stopQuery ();
@@ -1092,7 +1102,7 @@ public function exec(string $statement): int
1092
1102
try {
1093
1103
return $ connection ->exec ($ statement );
1094
1104
} catch (DriverException $ e ) {
1095
- $ this ->handleExceptionDuringQuery ($ e , $ statement );
1105
+ throw $ this ->convertExceptionDuringQuery ($ e , $ statement );
1096
1106
} finally {
1097
1107
if ($ logger !== null ) {
1098
1108
$ logger ->stopQuery ();
@@ -1580,15 +1590,12 @@ private function getBindingInfo($value, $type)
1580
1590
/**
1581
1591
* Resolves the parameters to a format which can be displayed.
1582
1592
*
1583
- * @internal This is a purely internal method. If you rely on this method, you are advised to
1584
- * copy/paste the code as this method may change, or be removed without prior notice.
1585
- *
1586
1593
* @param mixed[] $params
1587
1594
* @param array<int|string|null> $types
1588
1595
*
1589
1596
* @return mixed[]
1590
1597
*/
1591
- public function resolveParams (array $ params , array $ types )
1598
+ private function resolveParams (array $ params , array $ types ): array
1592
1599
{
1593
1600
$ resolvedParams = [];
1594
1601
@@ -1640,53 +1647,73 @@ public function createQueryBuilder()
1640
1647
*
1641
1648
* @param array<mixed> $params
1642
1649
* @param array<int|string|null> $types
1643
- *
1644
- * @throws DBALException
1645
- *
1646
- * @psalm-return never-return
1647
1650
*/
1648
- public function handleExceptionDuringQuery (Throwable $ e , string $ sql , array $ params = [], array $ types = []): void
1649
- {
1650
- $ this ->throw (
1651
- DBALException::driverExceptionDuringQuery (
1652
- $ this ->_driver ,
1653
- $ e ,
1654
- $ sql ,
1651
+ final public function convertExceptionDuringQuery (
1652
+ DriverException $ e ,
1653
+ string $ sql ,
1654
+ array $ params = [],
1655
+ array $ types = []
1656
+ ): DBALException {
1657
+ $ message = "An exception occurred while executing ' " . $ sql . "' " ;
1658
+
1659
+ if (count ($ params ) > 0 ) {
1660
+ $ message .= ' with params ' . $ this ->formatParameters (
1655
1661
$ this ->resolveParams ($ params , $ types )
1656
- )
1657
- );
1662
+ );
1663
+ }
1664
+
1665
+ $ message .= ": \n\n" . $ e ->getMessage ();
1666
+
1667
+ return $ this ->handleDriverException ($ e , $ message );
1658
1668
}
1659
1669
1660
1670
/**
1661
1671
* @internal
1662
- *
1663
- * @throws DBALException
1664
- *
1665
- * @psalm-return never-return
1666
1672
*/
1667
- public function handleDriverException ( Throwable $ e ): void
1673
+ final public function convertException ( DriverException $ e ): DBALException
1668
1674
{
1669
- $ this ->throw (
1670
- DBALException::driverException (
1671
- $ this ->_driver ,
1672
- $ e
1673
- )
1675
+ return $ this ->handleDriverException (
1676
+ $ e ,
1677
+ 'An exception occurred in driver: ' . $ e ->getMessage ()
1674
1678
);
1675
1679
}
1676
1680
1677
1681
/**
1678
- * @internal
1679
- *
1680
- * @throws DBALException
1682
+ * Returns a human-readable representation of an array of parameters.
1683
+ * This properly handles binary data by returning a hex representation.
1681
1684
*
1682
- * @psalm-return never-return
1685
+ * @param mixed[] $params
1683
1686
*/
1684
- private function throw ( DBALException $ e ): void
1687
+ private function formatParameters ( array $ params ): string
1685
1688
{
1686
- if ($ e instanceof ConnectionLost) {
1689
+ return '[ ' . implode (', ' , array_map (static function ($ param ): string {
1690
+ if (is_resource ($ param )) {
1691
+ return (string ) $ param ;
1692
+ }
1693
+
1694
+ $ json = @json_encode ($ param );
1695
+
1696
+ if (! is_string ($ json ) || $ json === 'null ' && is_string ($ param )) {
1697
+ // JSON encoding failed, this is not a UTF-8 string.
1698
+ return sprintf ('"%s" ' , preg_replace ('/.{2}/ ' , '\\x$0 ' , bin2hex ($ param )));
1699
+ }
1700
+
1701
+ return $ json ;
1702
+ }, $ params )) . '] ' ;
1703
+ }
1704
+
1705
+ private function handleDriverException (DriverException $ driverException , string $ message ): DBALException
1706
+ {
1707
+ if ($ this ->exceptionConverter === null ) {
1708
+ $ this ->exceptionConverter = $ this ->_driver ->getExceptionConverter ();
1709
+ }
1710
+
1711
+ $ exception = $ this ->exceptionConverter ->convert ($ message , $ driverException );
1712
+
1713
+ if ($ exception instanceof ConnectionLost) {
1687
1714
$ this ->close ();
1688
1715
}
1689
1716
1690
- throw $ e ;
1717
+ return $ exception ;
1691
1718
}
1692
1719
}
0 commit comments