@@ -2,6 +2,7 @@ use odbc_api::{
22 handles:: { slice_to_cow_utf8, Record } ,
33 Error as OdbcApiError ,
44} ;
5+ use sqlx_core:: error:: BoxDynError ;
56use std:: borrow:: Cow ;
67use std:: fmt:: { Display , Formatter , Result as FmtResult } ;
78
@@ -17,7 +18,7 @@ pub enum OdbcError {
1718
1819 /// Invalid local configuration.
1920 #[ error( "ODBC configuration error: {0}" ) ]
20- Configuration ( String ) ,
21+ Configuration ( # [ source ] BoxDynError ) ,
2122}
2223
2324impl From < OdbcApiError > for OdbcError {
@@ -30,11 +31,28 @@ impl From<OdbcError> for sqlx_core::Error {
3031 fn from ( error : OdbcError ) -> Self {
3132 match error {
3233 OdbcError :: Database ( error) => sqlx_core:: Error :: Database ( Box :: new ( error) ) ,
33- OdbcError :: Configuration ( message ) => sqlx_core:: Error :: Configuration ( message . into ( ) ) ,
34+ OdbcError :: Configuration ( error ) => sqlx_core:: Error :: Configuration ( error ) ,
3435 }
3536 }
3637}
3738
39+ pub ( crate ) fn connection_error ( error : OdbcApiError ) -> OdbcError {
40+ let local_setup_failure = match & error {
41+ OdbcApiError :: Diagnostics { record, .. } => {
42+ // Missing DSN, driver load failure, DRIVER syntax, or driver architecture mismatch.
43+ matches ! ( & record. state. 0 , b"IM002" | b"IM003" | b"IM012" | b"IM014" )
44+ }
45+ _ => false ,
46+ } ;
47+ if local_setup_failure {
48+ return OdbcError :: Configuration ( Box :: new ( error) ) ;
49+ }
50+ database_error_with_context (
51+ error,
52+ "failed to open ODBC connection using the supplied connection string" ,
53+ )
54+ }
55+
3856pub ( crate ) fn database_error_with_context (
3957 error : OdbcApiError ,
4058 context : impl Into < String > ,
@@ -161,6 +179,35 @@ mod tests {
161179 text. bytes ( ) . map ( C :: from) . collect ( )
162180 }
163181
182+ #[ test]
183+ fn only_known_local_connection_failures_are_configuration_errors ( ) {
184+ let diagnostic = |state| OdbcApiError :: Diagnostics {
185+ function : "SQLDriverConnect" ,
186+ record : Record {
187+ state : State ( state) ,
188+ native_error : 0 ,
189+ message : Vec :: new ( ) ,
190+ } ,
191+ } ;
192+
193+ for state in [ * b"IM002" , * b"IM003" , * b"IM012" , * b"IM014" ] {
194+ let OdbcError :: Configuration ( error) = connection_error ( diagnostic ( state) ) else {
195+ panic ! ( "{} should be a configuration error" , State ( state) . as_str( ) ) ;
196+ } ;
197+ assert ! ( matches!(
198+ error. downcast_ref:: <OdbcApiError >( ) ,
199+ Some ( OdbcApiError :: Diagnostics { record, .. } ) if record. state == State ( state)
200+ ) ) ;
201+ }
202+
203+ for state in [ * b"08001" , * b"28000" , * b"HY000" ] {
204+ let OdbcError :: Database ( error) = connection_error ( diagnostic ( state) ) else {
205+ panic ! ( "{} should be a database error" , State ( state) . as_str( ) ) ;
206+ } ;
207+ assert_eq ! ( error. code( ) . as_deref( ) , Some ( State ( state) . as_str( ) ) ) ;
208+ }
209+ }
210+
164211 #[ test]
165212 fn database_error_uses_odbc_diagnostics_for_message_and_code ( ) {
166213 let error = OdbcDatabaseError :: from ( OdbcApiError :: Diagnostics {
0 commit comments