@@ -31,7 +31,7 @@ public function connect(string $dsn, string $user = null, string $password = nul
3131 $ this ->pdo = new PDO ($ dsn , $ user , $ password , $ options );
3232 $ this ->pdo ->setAttribute (PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
3333 } catch (PDOException $ e ) {
34- throw Nette \Database \ConnectionException::from ( $ e );
34+ throw $ this -> convertException ( $ e , Nette \Database \ConnectionException::class );
3535 }
3636 }
3737
@@ -112,7 +112,30 @@ public function quote(string $string, int $type = PDO::PARAM_STR): string
112112 try {
113113 return $ this ->pdo ->quote ($ string , $ type );
114114 } catch (PDOException $ e ) {
115- throw DriverException::from ($ e );
115+ throw $ this ->convertException ($ e );
116+ }
117+ }
118+
119+
120+ public function convertException (\PDOException $ src , string $ class = null ): DriverException
121+ {
122+ if ($ src ->errorInfo ) {
123+ [$ sqlState , $ driverCode ] = $ src ->errorInfo ;
124+ } elseif (preg_match ('#SQLSTATE\[(.*?)\] \[(.*?)\] (.*)#A ' , $ src ->getMessage (), $ m )) {
125+ [, $ sqlState , $ driverCode ] = $ m ;
116126 }
127+
128+ $ class = $ this ->detectExceptionClass ($ src ) ?? $ class ?? DriverException::class;
129+ $ e = new $ class ($ src ->getMessage (), $ sqlState ?? $ src ->getCode (), $ src );
130+ if (isset ($ sqlState )) {
131+ $ e ->setDriverCode ($ sqlState , (int ) $ driverCode );
132+ }
133+ return $ e ;
134+ }
135+
136+
137+ public function detectExceptionClass (\PDOException $ e ): ?string
138+ {
139+ return null ;
117140 }
118141}
0 commit comments