Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tokio-Postgres] Error handling: Make tokio_postgres::error::Kind public #790 #1138

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions 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 {
},
}

#[derive(Debug, PartialEq)]
enum Kind {
/// The kind of error that occurred.
#[derive(Debug, Clone, PartialEq)]
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.clone()
}

/// 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