@@ -73,17 +73,17 @@ pub(crate) async fn connect(
7373 EngineConfig :: MySql => Engines :: MySql (
7474 MySql :: connect ( config. into ( ) )
7575 . await
76- . map_err ( |e| EnginesError ( e . into ( ) ) ) ?,
76+ . map_err ( EnginesError :: without_state ) ?,
7777 ) ,
7878 EngineConfig :: Postgres => Engines :: Postgres (
7979 PostgresSimple :: connect ( config. into ( ) )
8080 . await
81- . map_err ( |e| EnginesError ( e . into ( ) ) ) ?,
81+ . map_err ( EnginesError :: without_state ) ?,
8282 ) ,
8383 EngineConfig :: PostgresExtended => Engines :: PostgresExtended (
8484 PostgresExtended :: connect ( config. into ( ) )
8585 . await
86- . map_err ( |e| EnginesError ( e . into ( ) ) ) ?,
86+ . map_err ( EnginesError :: without_state ) ?,
8787 ) ,
8888 EngineConfig :: External ( cmd_tmpl) => {
8989 let ( host, port) = config. random_addr ( ) ;
@@ -98,24 +98,36 @@ pub(crate) async fn connect(
9898 Engines :: External (
9999 ExternalDriver :: connect ( cmd)
100100 . await
101- . map_err ( |e| EnginesError ( e . into ( ) ) ) ?,
101+ . map_err ( EnginesError :: without_state ) ?,
102102 )
103103 }
104104 } )
105105}
106106
107107#[ derive( Debug ) ]
108- pub ( crate ) struct EnginesError ( anyhow:: Error ) ;
108+ pub ( crate ) struct EnginesError {
109+ error : anyhow:: Error ,
110+ sqlstate : Option < String > ,
111+ }
112+
113+ impl EnginesError {
114+ fn without_state ( error : impl Into < anyhow:: Error > ) -> Self {
115+ Self {
116+ error : error. into ( ) ,
117+ sqlstate : None ,
118+ }
119+ }
120+ }
109121
110122impl Display for EnginesError {
111123 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
112- self . 0 . fmt ( f)
124+ self . error . fmt ( f)
113125 }
114126}
115127
116128impl std:: error:: Error for EnginesError {
117129 fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > {
118- self . 0 . source ( )
130+ self . error . source ( )
119131 }
120132}
121133
@@ -130,16 +142,21 @@ macro_rules! dispatch_engines {
130142 } } ;
131143}
132144
145+ fn error_sql_state < E : AsyncDB > ( _engine : & E , error : & E :: Error ) -> Option < String > {
146+ E :: error_sql_state ( error)
147+ }
148+
133149#[ async_trait]
134150impl AsyncDB for Engines {
135151 type Error = EnginesError ;
136152 type ColumnType = DefaultColumnType ;
137153
138154 async fn run ( & mut self , sql : & str ) -> Result < DBOutput < Self :: ColumnType > , Self :: Error > {
139155 dispatch_engines ! ( self , e, {
140- e. run( sql)
141- . await
142- . map_err( |e| EnginesError ( anyhow:: Error :: from( e) ) )
156+ e. run( sql) . await . map_err( |error| EnginesError {
157+ sqlstate: error_sql_state( e, & error) ,
158+ error: anyhow:: Error :: from( error) ,
159+ } )
143160 } )
144161 }
145162
@@ -158,4 +175,8 @@ impl AsyncDB for Engines {
158175 async fn shutdown ( & mut self ) {
159176 dispatch_engines ! ( self , e, { e. shutdown( ) . await } )
160177 }
178+
179+ fn error_sql_state ( err : & Self :: Error ) -> Option < String > {
180+ err. sqlstate . clone ( )
181+ }
161182}
0 commit comments