Skip to content

Commit 78818f9

Browse files
committed
Handle errors when running copy statements
1 parent a3039e6 commit 78818f9

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/stmt.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@ impl<'conn> Statement<'conn> {
310310

311311
let (format, column_formats) = match try!(conn.read_message()) {
312312
CopyInResponse { format, column_formats } => (format, column_formats),
313+
ErrorResponse { fields } => {
314+
try!(conn.wait_for_ready());
315+
return DbError::new(fields);
316+
}
313317
_ => {
314318
loop {
315319
match try!(conn.read_message()) {
@@ -425,6 +429,10 @@ impl<'conn> Statement<'conn> {
425429
io::ErrorKind::InvalidInput,
426430
"called `copy_out` on a non-`COPY TO STDOUT` statement")));
427431
}
432+
ErrorResponse { fields } => {
433+
try!(conn.wait_for_ready());
434+
return DbError::new(fields);
435+
}
428436
_ => {
429437
loop {
430438
match try!(conn.read_message()) {

tests/test.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,21 @@ fn test_copy_out() {
811811
or_panic!(conn.batch_execute("SELECT 1"));
812812
}
813813

814+
#[test]
815+
fn test_copy_out_error() {
816+
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", &SslMode::None));
817+
or_panic!(conn.batch_execute("
818+
CREATE TEMPORARY TABLE foo (id INT);
819+
INSERT INTO foo (id) VALUES (0), (1), (2), (3)"));
820+
let stmt = or_panic!(conn.prepare("COPY (SELECT id FROM foo ORDER BY id) TO STDOUT (OIDS)"));
821+
let mut buf = vec![];
822+
match stmt.copy_out(&[], &mut buf) {
823+
Ok(_) => panic!("unexpected success"),
824+
Err(Error::DbError(..)) => {}
825+
Err(e) => panic!("unexpected error {}", e),
826+
}
827+
}
828+
814829
#[test]
815830
// Just make sure the impls don't infinite loop
816831
fn test_generic_connection() {

0 commit comments

Comments
 (0)