Skip to content

Commit 2c4db01

Browse files
committed
Don't panic when trying to execute an empty query on postgresql.
1 parent 368674c commit 2c4db01

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

diesel/src/pg/connection/result.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ impl PgResult {
2424
internal_result: internal_result,
2525
})
2626
},
27+
PGRES_EMPTY_QUERY => {
28+
let error_message = "Received an empty query".to_string();
29+
Err(Error::DatabaseError(DatabaseErrorKind::__Unknown, Box::new(error_message)))
30+
},
2731
_ => {
2832
let error_kind = match get_result_field(internal_result.as_ptr(), ResultField::SqlState) {
2933
Some(error_codes::UNIQUE_VIOLATION) => DatabaseErrorKind::UniqueViolation,

diesel_tests/tests/internal_details.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,20 @@ fn query_which_cannot_be_transmitted_gives_proper_error_message() {
3232
Err(_) => panic!("We got back the wrong kind of error. This test is invalid."),
3333
}
3434
}
35+
36+
#[test]
37+
#[cfg(feature="postgres")]
38+
fn empty_query_gives_proper_error_instead_of_panicking() {
39+
use diesel::result::Error::DatabaseError;
40+
use diesel::result::DatabaseErrorKind::__Unknown;
41+
use diesel::expression::dsl::sql;
42+
43+
let connection = connection();
44+
let query = sql::<Integer>("");
45+
46+
match query.execute(&connection) {
47+
Ok(_) => panic!("We successfully executed an empty query"),
48+
Err(DatabaseError(__Unknown, info)) => assert_ne!("", info.message()),
49+
Err(_) => panic!("We got back the wrong kind of error. This test is invalid."),
50+
}
51+
}

0 commit comments

Comments
 (0)