From 6d869bef7e3db4523bb087ca836e628e2a8ab7c2 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Fri, 19 Mar 2021 23:36:24 -0700 Subject: [PATCH] feat(core): add Error::Client and ClientError to represent a general client error, replacing Error::Protocol --- sqlx-core/src/error.rs | 23 ++++++++++++++++++++++- sqlx-core/src/error/client.rs | 11 +++++++++++ sqlx-core/src/error/database.rs | 3 +-- sqlx-core/src/lib.rs | 6 +++--- 4 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 sqlx-core/src/error/client.rs diff --git a/sqlx-core/src/error.rs b/sqlx-core/src/error.rs index 1fa377d1..ad20e4ab 100644 --- a/sqlx-core/src/error.rs +++ b/sqlx-core/src/error.rs @@ -7,8 +7,10 @@ use either::Either; use crate::decode::Error as DecodeError; use crate::encode::Error as EncodeError; +mod client; mod database; +pub use client::ClientError; pub use database::DatabaseError; use crate::Column; @@ -25,9 +27,16 @@ pub enum Error { /// to be parsed. ConnectOptions { message: Cow<'static, str>, source: Option> }, - /// The database returned an error. + /// An error that was returned from the database, normally from the + /// execution of a SQL command. + /// Database(Box), + /// An error was identified on the client from the result of interacting + /// with the database. + /// + Client(Box), + /// An IO error returned while reading or writing a socket attached /// to the database server. /// @@ -92,6 +101,16 @@ impl Error { Self::ConnectOptions { message: message.into(), source: None } } + #[doc(hidden)] + pub fn client(err: impl ClientError) -> Self { + Self::Client(Box::new(err)) + } + + #[doc(hidden)] + pub fn database(err: impl DatabaseError) -> Self { + Self::Database(Box::new(err)) + } + #[doc(hidden)] pub fn column_decode(column: &impl Column, source: DecodeError) -> Self { crate::Error::ColumnDecode { @@ -109,6 +128,8 @@ impl Display for Error { Self::Database(source) => write!(f, "{}", source), + Self::Client(source) => write!(f, "{}", source), + Self::ConnectOptions { message, source: None } => { write!(f, "{}", message) } diff --git a/sqlx-core/src/error/client.rs b/sqlx-core/src/error/client.rs new file mode 100644 index 00000000..529aa3fa --- /dev/null +++ b/sqlx-core/src/error/client.rs @@ -0,0 +1,11 @@ +use std::error::Error as StdError; + +/// Representing an error that was identified by the client as a result +/// of interacting with the database. +/// +/// This can be anything from receiving invalid UTF-8 from the database +/// (where valid UTF-8 is expected) to being asked for interactive +/// authentication (where none is supported). +/// +#[allow(clippy::module_name_repetitions)] +pub trait ClientError: 'static + StdError + Send + Sync {} diff --git a/sqlx-core/src/error/database.rs b/sqlx-core/src/error/database.rs index 02db4c8e..a665fea8 100644 --- a/sqlx-core/src/error/database.rs +++ b/sqlx-core/src/error/database.rs @@ -1,7 +1,6 @@ use std::error::Error as StdError; -/// `DatabaseError` is a trait representing an error that was returned from -/// the database. +/// Representing an error that was returned from the database. /// /// Provides abstract access to information returned from the database about /// the error. diff --git a/sqlx-core/src/lib.rs b/sqlx-core/src/lib.rs index 919666a6..ff8b5237 100644 --- a/sqlx-core/src/lib.rs +++ b/sqlx-core/src/lib.rs @@ -22,12 +22,12 @@ mod acquire; pub mod arguments; mod close; -mod describe; mod column; mod connect; mod connection; pub mod database; pub mod decode; +mod describe; pub mod encode; mod error; mod execute; @@ -65,8 +65,9 @@ pub use connect::Connect; pub use connection::Connection; pub use database::Database; pub use decode::Decode; +pub use describe::Describe; pub use encode::Encode; -pub use error::{DatabaseError, Error, Result}; +pub use error::{ClientError, DatabaseError, Error, Result}; pub use execute::Execute; pub use executor::Executor; pub use from_row::FromRow; @@ -86,4 +87,3 @@ pub use runtime::Runtime; #[cfg(feature = "tokio")] pub use runtime::Tokio; pub use type_info::TypeInfo; -pub use describe::Describe;