@@ -40,7 +40,7 @@ public function connect(string $dsn, string $user = null, string $password = nul
4040 $ this ->pdo = new PDO ($ dsn , $ user , $ password , $ options );
4141 $ this ->pdo ->setAttribute (PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
4242 } catch (PDOException $ e ) {
43- throw Nette \Database \ConnectionException::from ( $ e );
43+ throw $ this -> convertException ( $ e , Nette \Database \ConnectionException::class );
4444 }
4545 }
4646
@@ -121,7 +121,30 @@ public function quote(string $string, int $type = PDO::PARAM_STR): string
121121 try {
122122 return $ this ->pdo ->quote ($ string , $ type );
123123 } catch (PDOException $ e ) {
124- throw DriverException::from ($ e );
124+ throw $ this ->convertException ($ e );
125+ }
126+ }
127+
128+
129+ public function convertException (\PDOException $ src , string $ class = null ): DriverException
130+ {
131+ if ($ src ->errorInfo ) {
132+ [$ sqlState , $ driverCode ] = $ src ->errorInfo ;
133+ } elseif (preg_match ('#SQLSTATE\[(.*?)\] \[(.*?)\] (.*)#A ' , $ src ->getMessage (), $ m )) {
134+ [, $ sqlState , $ driverCode ] = $ m ;
125135 }
136+
137+ $ class = $ this ->detectExceptionClass ($ src ) ?? $ class ?? DriverException::class;
138+ $ e = new $ class ($ src ->getMessage (), $ sqlState ?? $ src ->getCode (), $ src );
139+ if (isset ($ sqlState )) {
140+ $ e ->setDriverCode ($ sqlState , (int ) $ driverCode );
141+ }
142+ return $ e ;
143+ }
144+
145+
146+ public function detectExceptionClass (\PDOException $ e ): ?string
147+ {
148+ return null ;
126149 }
127150}
0 commit comments