Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes fromall 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 deletionstokio-postgres/src/error/mod.rs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletiontokio-postgres/src/lib.rs
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionstokio-postgres/tests/test/main.rs
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp