File tree Expand file tree Collapse file tree 5 files changed +31
-6
lines changed Expand file tree Collapse file tree 5 files changed +31
-6
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,9 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/
10
10
11
11
* ` NonAggregate ` can now be derived for simple cases.
12
12
13
+ * Added ` DatabaseErrorKind::ReadOnlyTransaction ` to allow applications to
14
+ handle errors caused by writing when only allowed to read.
15
+
13
16
### Removed
14
17
15
18
* All previously deprecated items have been removed.
Original file line number Diff line number Diff line change @@ -147,6 +147,7 @@ impl Statement {
147
147
match last_error_number {
148
148
1062 | 1586 | 1859 => DatabaseErrorKind :: UniqueViolation ,
149
149
1216 | 1217 | 1451 | 1452 | 1830 | 1834 => DatabaseErrorKind :: ForeignKeyViolation ,
150
+ 1792 => DatabaseErrorKind :: ReadOnlyTransaction ,
150
151
_ => DatabaseErrorKind :: __Unknown,
151
152
}
152
153
}
Original file line number Diff line number Diff line change @@ -34,12 +34,9 @@ impl PgResult {
34
34
let error_kind =
35
35
match get_result_field ( internal_result. as_ptr ( ) , ResultField :: SqlState ) {
36
36
Some ( error_codes:: UNIQUE_VIOLATION ) => DatabaseErrorKind :: UniqueViolation ,
37
- Some ( error_codes:: FOREIGN_KEY_VIOLATION ) => {
38
- DatabaseErrorKind :: ForeignKeyViolation
39
- }
40
- Some ( error_codes:: SERIALIZATION_FAILURE ) => {
41
- DatabaseErrorKind :: SerializationFailure
42
- }
37
+ Some ( error_codes:: FOREIGN_KEY_VIOLATION ) => DatabaseErrorKind :: ForeignKeyViolation ,
38
+ Some ( error_codes:: SERIALIZATION_FAILURE ) => DatabaseErrorKind :: SerializationFailure ,
39
+ Some ( error_codes:: READ_ONLY_TRANSACTION ) => DatabaseErrorKind :: ReadOnlyTransaction ,
43
40
_ => DatabaseErrorKind :: __Unknown,
44
41
} ;
45
42
let error_information = Box :: new ( PgErrorInformation ( internal_result) ) ;
@@ -167,4 +164,5 @@ mod error_codes {
167
164
pub const UNIQUE_VIOLATION : & str = "23505" ;
168
165
pub const FOREIGN_KEY_VIOLATION : & str = "23503" ;
169
166
pub const SERIALIZATION_FAILURE : & str = "40001" ;
167
+ pub const READ_ONLY_TRANSACTION : & str = "25006" ;
170
168
}
Original file line number Diff line number Diff line change @@ -106,6 +106,13 @@ pub enum DatabaseErrorKind {
106
106
/// transaction isolation levels for other backends.
107
107
SerializationFailure ,
108
108
109
+ /// The command could not be completed because the transaction was read
110
+ /// only.
111
+ ///
112
+ /// This error will also be returned for `SELECT` statements which attempted
113
+ /// to lock the rows.
114
+ ReadOnlyTransaction ,
115
+
109
116
#[ doc( hidden) ]
110
117
__Unknown, // Match against _ instead, more variants may be added in the future
111
118
}
Original file line number Diff line number Diff line change @@ -176,3 +176,19 @@ fn isolation_errors_are_detected() {
176
176
assert_matches ! ( results[ 0 ] , Ok ( _) ) ;
177
177
assert_matches ! ( results[ 1 ] , Err ( DatabaseError ( SerializationFailure , _) ) ) ;
178
178
}
179
+
180
+ #[ test]
181
+ #[ cfg( not( feature = "sqlite" ) ) ]
182
+ fn read_only_errors_are_detected ( ) {
183
+ use diesel:: result:: DatabaseErrorKind :: ReadOnlyTransaction ;
184
+
185
+ let conn = connection_without_transaction ( ) ;
186
+ conn. execute ( "START TRANSACTION READ ONLY" )
187
+ . unwrap ( ) ;
188
+
189
+ let result = users:: table
190
+ . for_update ( )
191
+ . load :: < User > ( & conn) ;
192
+
193
+ assert_matches ! ( result, Err ( DatabaseError ( ReadOnlyTransaction , _) ) ) ;
194
+ }
You can’t perform that action at this time.
0 commit comments