Skip to content

Commit 68bd96c

Browse files
Make tokio_postgres::error::Kind public and add its getter (sfackler#790)
1 parent 98f5a11 commit 68bd96c

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

tokio-postgres/src/error/mod.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,25 +336,43 @@ pub enum ErrorPosition {
336336
},
337337
}
338338

339+
/// The kind of error that occurred.
339340
#[derive(Debug, PartialEq)]
340-
enum Kind {
341+
pub enum Kind {
342+
/// An I/O error occurred.
341343
Io,
344+
/// An unexpected message from the server.
342345
UnexpectedMessage,
346+
/// An error occurred during TLS handshake.
343347
Tls,
348+
/// An error occurred while serializing a parameter.
344349
ToSql(usize),
350+
/// An error occurred while deserializing a parameter.
345351
FromSql(usize),
352+
/// An error occurred with a specific column.
346353
Column(String),
354+
/// An error occurred with the parameters.
347355
Parameters(usize, usize),
356+
/// The connection is closed.
348357
Closed,
358+
/// A generic database error occurred.
349359
Db,
360+
/// An error occurred while parsing.
350361
Parse,
362+
/// An error occurred while encoding.
351363
Encode,
364+
/// An authentication error occurred.
352365
Authentication,
366+
/// An error occurred while parsing the configuration.
353367
ConfigParse,
368+
/// An error occurred with the configuration.
354369
Config,
370+
/// An error occurred while counting rows.
355371
RowCount,
356372
#[cfg(feature = "runtime")]
373+
/// An error occurred while connecting.
357374
Connect,
375+
/// A timeout occurred.
358376
Timeout,
359377
}
360378

@@ -418,6 +436,11 @@ impl Error {
418436
self.0.cause
419437
}
420438

439+
/// Returns the kind of this error.
440+
pub fn kind(&self) -> &Kind {
441+
&self.0.kind
442+
}
443+
421444
/// Returns the source of this error if it was a `DbError`.
422445
///
423446
/// This is a simple convenience method.

tokio-postgres/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub use crate::connection::Connection;
125125
pub use crate::copy_in::CopyInSink;
126126
pub use crate::copy_out::CopyOutStream;
127127
use crate::error::DbError;
128-
pub use crate::error::Error;
128+
pub use crate::error::{Error, Kind};
129129
pub use crate::generic_client::GenericClient;
130130
pub use crate::portal::Portal;
131131
pub use crate::query::RowStream;

tokio-postgres/tests/test/main.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,32 @@ async fn query_opt() {
927927
.unwrap();
928928
}
929929

930+
#[tokio::test]
931+
async fn empty_query_one() {
932+
let client = connect("user=postgres").await;
933+
934+
client
935+
.batch_execute(
936+
"
937+
CREATE TEMPORARY TABLE foo (
938+
name TEXT
939+
);
940+
INSERT INTO foo (name) VALUES ('alice'), ('bob'), ('carol');
941+
",
942+
)
943+
.await
944+
.unwrap();
945+
946+
let res = client
947+
.query_one("SELECT * FROM foo WHERE name = $1", &[&"not there"])
948+
.await;
949+
assert!(res.is_err());
950+
assert_eq!(
951+
res.err().unwrap().kind(),
952+
&tokio_postgres::error::Kind::RowCount
953+
);
954+
}
955+
930956
#[tokio::test]
931957
async fn deferred_constraint() {
932958
let client = connect("user=postgres").await;

0 commit comments

Comments
 (0)