feat(core): add Error::Client and ClientError to represent a general client error, replacing Error::Protocol

This commit is contained in:
Ryan Leckey 2021-03-19 23:36:24 -07:00
parent 2b8ca88b87
commit 6d869bef7e
No known key found for this signature in database
GPG Key ID: F8AA68C235AB08C9
4 changed files with 37 additions and 6 deletions

View File

@ -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<Box<dyn StdError + Send + Sync>> },
/// The database returned an error.
/// An error that was returned from the database, normally from the
/// execution of a SQL command.
///
Database(Box<dyn DatabaseError>),
/// An error was identified on the client from the result of interacting
/// with the database.
///
Client(Box<dyn ClientError>),
/// 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)
}

View File

@ -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 {}

View File

@ -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.

View File

@ -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;