From 68bd96c0456bed1c2f8d15808145e9a3deea38d1 Mon Sep 17 00:00:00 2001 From: Baptiste Roseau Date: Mon, 6 May 2024 13:18:28 +0200 Subject: [PATCH 1/2] Make tokio_postgres::error::Kind public and add its getter (#790) --- tokio-postgres/src/error/mod.rs | 25 ++++++++++++++++++++++++- tokio-postgres/src/lib.rs | 2 +- tokio-postgres/tests/test/main.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/tokio-postgres/src/error/mod.rs b/tokio-postgres/src/error/mod.rs index f1e2644c6..4dcf063f3 100644 --- a/tokio-postgres/src/error/mod.rs +++ b/tokio-postgres/src/error/mod.rs @@ -336,25 +336,43 @@ pub enum ErrorPosition { }, } +/// The kind of error that occurred. #[derive(Debug, PartialEq)] -enum Kind { +pub enum Kind { + /// An I/O error occurred. Io, + /// An unexpected message from the server. UnexpectedMessage, + /// An error occurred during TLS handshake. Tls, + /// An error occurred while serializing a parameter. ToSql(usize), + /// An error occurred while deserializing a parameter. FromSql(usize), + /// An error occurred with a specific column. Column(String), + /// An error occurred with the parameters. Parameters(usize, usize), + /// The connection is closed. Closed, + /// A generic database error occurred. Db, + /// An error occurred while parsing. Parse, + /// An error occurred while encoding. Encode, + /// An authentication error occurred. Authentication, + /// An error occurred while parsing the configuration. ConfigParse, + /// An error occurred with the configuration. Config, + /// An error occurred while counting rows. RowCount, #[cfg(feature = "runtime")] + /// An error occurred while connecting. Connect, + /// A timeout occurred. Timeout, } @@ -418,6 +436,11 @@ impl Error { self.0.cause } + /// Returns the kind of this error. + pub fn kind(&self) -> &Kind { + &self.0.kind + } + /// Returns the source of this error if it was a `DbError`. /// /// This is a simple convenience method. diff --git a/tokio-postgres/src/lib.rs b/tokio-postgres/src/lib.rs index 2973d33b0..f718bcd16 100644 --- a/tokio-postgres/src/lib.rs +++ b/tokio-postgres/src/lib.rs @@ -125,7 +125,7 @@ pub use crate::connection::Connection; pub use crate::copy_in::CopyInSink; pub use crate::copy_out::CopyOutStream; use crate::error::DbError; -pub use crate::error::Error; +pub use crate::error::{Error, Kind}; pub use crate::generic_client::GenericClient; pub use crate::portal::Portal; pub use crate::query::RowStream; diff --git a/tokio-postgres/tests/test/main.rs b/tokio-postgres/tests/test/main.rs index 737f46631..26e68d591 100644 --- a/tokio-postgres/tests/test/main.rs +++ b/tokio-postgres/tests/test/main.rs @@ -927,6 +927,32 @@ async fn query_opt() { .unwrap(); } +#[tokio::test] +async fn empty_query_one() { + let client = connect("user=postgres").await; + + client + .batch_execute( + " + CREATE TEMPORARY TABLE foo ( + name TEXT + ); + INSERT INTO foo (name) VALUES ('alice'), ('bob'), ('carol'); + ", + ) + .await + .unwrap(); + + let res = client + .query_one("SELECT * FROM foo WHERE name = $1", &[&"not there"]) + .await; + assert!(res.is_err()); + assert_eq!( + res.err().unwrap().kind(), + &tokio_postgres::error::Kind::RowCount + ); +} + #[tokio::test] async fn deferred_constraint() { let client = connect("user=postgres").await; From 407db86608fe89521170923a8af6a002d76d5f79 Mon Sep 17 00:00:00 2001 From: Baptiste Roseau Date: Mon, 6 May 2024 13:20:32 +0200 Subject: [PATCH 2/2] Clone tokio_postgres::error::Kind instead of returning a reference (#790) --- tokio-postgres/src/error/mod.rs | 6 +++--- tokio-postgres/tests/test/main.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tokio-postgres/src/error/mod.rs b/tokio-postgres/src/error/mod.rs index 4dcf063f3..a0b147934 100644 --- a/tokio-postgres/src/error/mod.rs +++ b/tokio-postgres/src/error/mod.rs @@ -337,7 +337,7 @@ pub enum ErrorPosition { } /// The kind of error that occurred. -#[derive(Debug, PartialEq)] +#[derive(Debug, Clone, PartialEq)] pub enum Kind { /// An I/O error occurred. Io, @@ -437,8 +437,8 @@ impl Error { } /// Returns the kind of this error. - pub fn kind(&self) -> &Kind { - &self.0.kind + pub fn kind(&self) -> Kind { + self.0.kind.clone() } /// Returns the source of this error if it was a `DbError`. diff --git a/tokio-postgres/tests/test/main.rs b/tokio-postgres/tests/test/main.rs index 26e68d591..b9047e195 100644 --- a/tokio-postgres/tests/test/main.rs +++ b/tokio-postgres/tests/test/main.rs @@ -949,7 +949,7 @@ async fn empty_query_one() { assert!(res.is_err()); assert_eq!( res.err().unwrap().kind(), - &tokio_postgres::error::Kind::RowCount + tokio_postgres::error::Kind::RowCount ); }