Skip to content

Commit

Permalink
Make tokio_postgres::error::Kind public and add its getter (sfackler#790
Browse files Browse the repository at this point in the history
)
  • Loading branch information
BaptisteRoseau committed May 6, 2024
1 parent 98f5a11 commit 68bd96c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
25 changes: 24 additions & 1 deletion tokio-postgres/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion tokio-postgres/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 26 additions & 0 deletions tokio-postgres/tests/test/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 68bd96c

Please sign in to comment.