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 ;
@@ -467,8 +477,8 @@ public function fetchAssociative(string $query, array $params = [], array $types
467
477
{
468
478
try {
469
479
return $ this ->executeQuery ($ query , $ params , $ types )->fetchAssociative ();
470
- } catch (Throwable $ e ) {
471
- $ this ->handleExceptionDuringQuery ($ e , $ query , $ params , $ types );
480
+ } catch (DriverException $ e ) {
481
+ throw $ this ->convertExceptionDuringQuery ($ e , $ query , $ params , $ types );
472
482
}
473
483
}
474
484
@@ -488,8 +498,8 @@ public function fetchNumeric(string $query, array $params = [], array $types = [
488
498
{
489
499
try {
490
500
return $ this ->executeQuery ($ query , $ params , $ types )->fetchNumeric ();
491
- } catch (Throwable $ e ) {
492
- $ this ->handleExceptionDuringQuery ($ e , $ query , $ params , $ types );
501
+ } catch (DriverException $ e ) {
502
+ throw $ this ->convertExceptionDuringQuery ($ e , $ query , $ params , $ types );
493
503
}
494
504
}
495
505
@@ -509,8 +519,8 @@ public function fetchOne(string $query, array $params = [], array $types = [])
509
519
{
510
520
try {
511
521
return $ this ->executeQuery ($ query , $ params , $ types )->fetchOne ();
512
- } catch (Throwable $ e ) {
513
- $ this ->handleExceptionDuringQuery ($ e , $ query , $ params , $ types );
522
+ } catch (DriverException $ e ) {
523
+ throw $ this ->convertExceptionDuringQuery ($ e , $ query , $ params , $ types );
514
524
}
515
525
}
516
526
@@ -771,8 +781,8 @@ public function fetchAllNumeric(string $query, array $params = [], array $types
771
781
{
772
782
try {
773
783
return $ this ->executeQuery ($ query , $ params , $ types )->fetchAllNumeric ();
774
- } catch (Throwable $ e ) {
775
- $ this ->handleExceptionDuringQuery ($ e , $ query , $ params , $ types );
784
+ } catch (DriverException $ e ) {
785
+ throw $ this ->convertExceptionDuringQuery ($ e , $ query , $ params , $ types );
776
786
}
777
787
}
778
788
@@ -791,8 +801,8 @@ public function fetchAllAssociative(string $query, array $params = [], array $ty
791
801
{
792
802
try {
793
803
return $ this ->executeQuery ($ query , $ params , $ types )->fetchAllAssociative ();
794
- } catch (Throwable $ e ) {
795
- $ this ->handleExceptionDuringQuery ($ e , $ query , $ params , $ types );
804
+ } catch (DriverException $ e ) {
805
+ throw $ this ->convertExceptionDuringQuery ($ e , $ query , $ params , $ types );
796
806
}
797
807
}
798
808
@@ -811,8 +821,8 @@ public function fetchFirstColumn(string $query, array $params = [], array $types
811
821
{
812
822
try {
813
823
return $ this ->executeQuery ($ query , $ params , $ types )->fetchFirstColumn ();
814
- } catch (Throwable $ e ) {
815
- $ this ->handleExceptionDuringQuery ($ e , $ query , $ params , $ types );
824
+ } catch (DriverException $ e ) {
825
+ throw $ this ->convertExceptionDuringQuery ($ e , $ query , $ params , $ types );
816
826
}
817
827
}
818
828
@@ -835,8 +845,8 @@ public function iterateNumeric(string $query, array $params = [], array $types =
835
845
while (($ row = $ result ->fetchNumeric ()) !== false ) {
836
846
yield $ row ;
837
847
}
838
- } catch (Throwable $ e ) {
839
- $ this ->handleExceptionDuringQuery ($ e , $ query , $ params , $ types );
848
+ } catch (DriverException $ e ) {
849
+ throw $ this ->convertExceptionDuringQuery ($ e , $ query , $ params , $ types );
840
850
}
841
851
}
842
852
@@ -859,8 +869,8 @@ public function iterateAssociative(string $query, array $params = [], array $typ
859
869
while (($ row = $ result ->fetchAssociative ()) !== false ) {
860
870
yield $ row ;
861
871
}
862
- } catch (Throwable $ e ) {
863
- $ this ->handleExceptionDuringQuery ($ e , $ query , $ params , $ types );
872
+ } catch (DriverException $ e ) {
873
+ throw $ this ->convertExceptionDuringQuery ($ e , $ query , $ params , $ types );
864
874
}
865
875
}
866
876
@@ -883,8 +893,8 @@ public function iterateColumn(string $query, array $params = [], array $types =
883
893
while (($ value = $ result ->fetchOne ()) !== false ) {
884
894
yield $ value ;
885
895
}
886
- } catch (Throwable $ e ) {
887
- $ this ->handleExceptionDuringQuery ($ e , $ query , $ params , $ types );
896
+ } catch (DriverException $ e ) {
897
+ throw $ this ->convertExceptionDuringQuery ($ e , $ query , $ params , $ types );
888
898
}
889
899
}
890
900
@@ -899,11 +909,7 @@ public function iterateColumn(string $query, array $params = [], array $types =
899
909
*/
900
910
public function prepare (string $ sql ): DriverStatement
901
911
{
902
- try {
903
- return new Statement ($ sql , $ this );
904
- } catch (Throwable $ e ) {
905
- $ this ->handleExceptionDuringQuery ($ e , $ sql );
906
- }
912
+ return new Statement ($ sql , $ this );
907
913
}
908
914
909
915
/**
@@ -952,8 +958,8 @@ public function executeQuery(
952
958
}
953
959
954
960
return new Result ($ result , $ this );
955
- } catch (Throwable $ e ) {
956
- $ this ->handleExceptionDuringQuery ($ e , $ query , $ params , $ types );
961
+ } catch (DriverException $ e ) {
962
+ throw $ this ->convertExceptionDuringQuery ($ e , $ query , $ params , $ types );
957
963
} finally {
958
964
if ($ logger !== null ) {
959
965
$ logger ->stopQuery ();
@@ -1010,6 +1016,9 @@ public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qc
1010
1016
return new Result ($ result , $ this );
1011
1017
}
1012
1018
1019
+ /**
1020
+ * @throws DBALException
1021
+ */
1013
1022
public function query (string $ sql ): DriverResult
1014
1023
{
1015
1024
$ connection = $ this ->getWrappedConnection ();
@@ -1021,8 +1030,8 @@ public function query(string $sql): DriverResult
1021
1030
1022
1031
try {
1023
1032
return $ connection ->query ($ sql );
1024
- } catch (Throwable $ e ) {
1025
- $ this ->handleExceptionDuringQuery ($ e , $ sql );
1033
+ } catch (DriverException $ e ) {
1034
+ throw $ this ->convertExceptionDuringQuery ($ e , $ sql );
1026
1035
} finally {
1027
1036
if ($ logger !== null ) {
1028
1037
$ logger ->stopQuery ();
@@ -1067,15 +1076,18 @@ public function executeUpdate(string $query, array $params = [], array $types =
1067
1076
}
1068
1077
1069
1078
return $ connection ->exec ($ query );
1070
- } catch (Throwable $ e ) {
1071
- $ this ->handleExceptionDuringQuery ($ e , $ query , $ params , $ types );
1079
+ } catch (DriverException $ e ) {
1080
+ throw $ this ->convertExceptionDuringQuery ($ e , $ query , $ params , $ types );
1072
1081
} finally {
1073
1082
if ($ logger !== null ) {
1074
1083
$ logger ->stopQuery ();
1075
1084
}
1076
1085
}
1077
1086
}
1078
1087
1088
+ /**
1089
+ * @throws DBALException
1090
+ */
1079
1091
public function exec (string $ statement ): int
1080
1092
{
1081
1093
$ connection = $ this ->getWrappedConnection ();
@@ -1087,8 +1099,8 @@ public function exec(string $statement): int
1087
1099
1088
1100
try {
1089
1101
return $ connection ->exec ($ statement );
1090
- } catch (Throwable $ e ) {
1091
- $ this ->handleExceptionDuringQuery ($ e , $ statement );
1102
+ } catch (DriverException $ e ) {
1103
+ throw $ this ->convertExceptionDuringQuery ($ e , $ statement );
1092
1104
} finally {
1093
1105
if ($ logger !== null ) {
1094
1106
$ logger ->stopQuery ();
@@ -1573,15 +1585,12 @@ private function getBindingInfo($value, $type)
1573
1585
/**
1574
1586
* Resolves the parameters to a format which can be displayed.
1575
1587
*
1576
- * @internal This is a purely internal method. If you rely on this method, you are advised to
1577
- * copy/paste the code as this method may change, or be removed without prior notice.
1578
- *
1579
1588
* @param mixed[] $params
1580
1589
* @param array<int|string|null> $types
1581
1590
*
1582
1591
* @return mixed[]
1583
1592
*/
1584
- public function resolveParams (array $ params , array $ types )
1593
+ private function resolveParams (array $ params , array $ types ): array
1585
1594
{
1586
1595
$ resolvedParams = [];
1587
1596
@@ -1633,53 +1642,73 @@ public function createQueryBuilder()
1633
1642
*
1634
1643
* @param array<mixed> $params
1635
1644
* @param array<int|string|null> $types
1636
- *
1637
- * @throws DBALException
1638
- *
1639
- * @psalm-return never-return
1640
1645
*/
1641
- public function handleExceptionDuringQuery (Throwable $ e , string $ sql , array $ params = [], array $ types = []): void
1642
- {
1643
- $ this ->throw (
1644
- DBALException::driverExceptionDuringQuery (
1645
- $ this ->_driver ,
1646
- $ e ,
1647
- $ sql ,
1646
+ final public function convertExceptionDuringQuery (
1647
+ DriverException $ e ,
1648
+ string $ sql ,
1649
+ array $ params = [],
1650
+ array $ types = []
1651
+ ): DBALException {
1652
+ $ message = "An exception occurred while executing ' " . $ sql . "' " ;
1653
+
1654
+ if (count ($ params ) > 0 ) {
1655
+ $ message .= ' with params ' . $ this ->formatParameters (
1648
1656
$ this ->resolveParams ($ params , $ types )
1649
- )
1650
- );
1657
+ );
1658
+ }
1659
+
1660
+ $ message .= ": \n\n" . $ e ->getMessage ();
1661
+
1662
+ return $ this ->handleDriverException ($ e , $ message );
1651
1663
}
1652
1664
1653
1665
/**
1654
1666
* @internal
1655
- *
1656
- * @throws DBALException
1657
- *
1658
- * @psalm-return never-return
1659
1667
*/
1660
- public function handleDriverException ( Throwable $ e ): void
1668
+ final public function convertException ( DriverException $ e ): DBALException
1661
1669
{
1662
- $ this ->throw (
1663
- DBALException::driverException (
1664
- $ this ->_driver ,
1665
- $ e
1666
- )
1670
+ return $ this ->handleDriverException (
1671
+ $ e ,
1672
+ 'An exception occurred in driver: ' . $ e ->getMessage ()
1667
1673
);
1668
1674
}
1669
1675
1670
1676
/**
1671
- * @internal
1672
- *
1673
- * @throws DBALException
1677
+ * Returns a human-readable representation of an array of parameters.
1678
+ * This properly handles binary data by returning a hex representation.
1674
1679
*
1675
- * @psalm-return never-return
1680
+ * @param mixed[] $params
1676
1681
*/
1677
- private function throw (DBALException $ e ): void
1682
+ private function formatParameters (array $ params ): string
1683
+ {
1684
+ return '[ ' . implode (', ' , array_map (static function ($ param ): string {
1685
+ if (is_resource ($ param )) {
1686
+ return (string ) $ param ;
1687
+ }
1688
+
1689
+ $ json = @json_encode ($ param );
1690
+
1691
+ if (! is_string ($ json ) || $ json === 'null ' && is_string ($ param )) {
1692
+ // JSON encoding failed, this is not a UTF-8 string.
1693
+ return sprintf ('"%s" ' , preg_replace ('/.{2}/ ' , '\\x$0 ' , bin2hex ($ param )));
1694
+ }
1695
+
1696
+ return $ json ;
1697
+ }, $ params )) . '] ' ;
1698
+ }
1699
+
1700
+ private function handleDriverException (DriverException $ driverException , string $ message ): DBALException
1678
1701
{
1679
- if ($ e instanceof ConnectionLost) {
1702
+ if ($ this ->exceptionConverter === null ) {
1703
+ $ this ->exceptionConverter = $ this ->_driver ->getExceptionConverter ();
1704
+ }
1705
+
1706
+ $ exception = $ this ->exceptionConverter ->convert ($ message , $ driverException );
1707
+
1708
+ if ($ exception instanceof ConnectionLost) {
1680
1709
$ this ->close ();
1681
1710
}
1682
1711
1683
- throw $ e ;
1712
+ return $ exception ;
1684
1713
}
1685
1714
}
0 commit comments