File tree Expand file tree Collapse file tree 4 files changed +36
-3
lines changed Expand file tree Collapse file tree 4 files changed +36
-3
lines changed Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ use crate::connection::establish::EstablishParams;
28
28
use crate :: connection:: worker:: ConnectionWorker ;
29
29
use crate :: options:: OptimizeOnClose ;
30
30
use crate :: statement:: VirtualStatement ;
31
- use crate :: { Sqlite , SqliteConnectOptions } ;
31
+ use crate :: { Sqlite , SqliteConnectOptions , SqliteError } ;
32
32
33
33
pub ( crate ) mod collation;
34
34
pub ( crate ) mod describe;
@@ -542,6 +542,10 @@ impl LockedSqliteHandle<'_> {
542
542
pub fn remove_rollback_hook ( & mut self ) {
543
543
self . guard . remove_rollback_hook ( ) ;
544
544
}
545
+
546
+ pub fn last_error ( & mut self ) -> Option < SqliteError > {
547
+ SqliteError :: try_new ( self . guard . handle . as_ptr ( ) )
548
+ }
545
549
}
546
550
547
551
impl Drop for ConnectionState {
Original file line number Diff line number Diff line change @@ -23,9 +23,17 @@ pub struct SqliteError {
23
23
24
24
impl SqliteError {
25
25
pub ( crate ) fn new ( handle : * mut sqlite3 ) -> Self {
26
+ Self :: try_new ( handle) . expect ( "There should be an error" )
27
+ }
28
+
29
+ pub ( crate ) fn try_new ( handle : * mut sqlite3 ) -> Option < Self > {
26
30
// returns the extended result code even when extended result codes are disabled
27
31
let code: c_int = unsafe { sqlite3_extended_errcode ( handle) } ;
28
32
33
+ if code == 0 {
34
+ return None ;
35
+ }
36
+
29
37
// return English-language text that describes the error
30
38
let message = unsafe {
31
39
let msg = sqlite3_errmsg ( handle) ;
@@ -34,10 +42,10 @@ impl SqliteError {
34
42
from_utf8_unchecked ( CStr :: from_ptr ( msg) . to_bytes ( ) )
35
43
} ;
36
44
37
- Self {
45
+ Some ( Self {
38
46
code,
39
47
message : message. to_owned ( ) ,
40
- }
48
+ } )
41
49
}
42
50
43
51
/// For errors during extension load, the error message is supplied via a separate pointer
Original file line number Diff line number Diff line change @@ -1172,3 +1172,24 @@ async fn test_multiple_set_preupdate_hook_calls_drop_old_handler() -> anyhow::Re
1172
1172
assert_eq ! ( 1 , Arc :: strong_count( & ref_counted_object) ) ;
1173
1173
Ok ( ( ) )
1174
1174
}
1175
+
1176
+ #[ sqlx_macros:: test]
1177
+ async fn test_get_last_error ( ) -> anyhow:: Result < ( ) > {
1178
+ let mut conn = new :: < Sqlite > ( ) . await ?;
1179
+
1180
+ let _ = sqlx:: query ( "select 1" ) . fetch_one ( & mut conn) . await ?;
1181
+
1182
+ {
1183
+ let mut handle = conn. lock_handle ( ) . await ?;
1184
+ assert ! ( handle. last_error( ) . is_none( ) ) ;
1185
+ }
1186
+
1187
+ let _ = sqlx:: query ( "invalid statement" ) . fetch_one ( & mut conn) . await ;
1188
+
1189
+ {
1190
+ let mut handle = conn. lock_handle ( ) . await ?;
1191
+ assert ! ( handle. last_error( ) . is_some( ) ) ;
1192
+ }
1193
+
1194
+ Ok ( ( ) )
1195
+ }
You can’t perform that action at this time.
0 commit comments