Skip to content

Commit

Permalink
remove auth error type (#1147)
Browse files Browse the repository at this point in the history
  • Loading branch information
fakeshadow authored Oct 5, 2024
1 parent c40616e commit 033e2f9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 32 deletions.
1 change: 1 addition & 0 deletions postgres/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# unreleased 0.3.0
## Remove
- remove `ExecuteMut` trait. It's role is replaced by `impl Execute<&mut C>`
- remove `error::AuthenticationError` type. It's error condition is covered by `error::ConfigError`

## Change
- change `pool::Pool`'s dead connection detection lifecycle.
Expand Down
29 changes: 6 additions & 23 deletions postgres/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ impl From<FromSqlError> for Error {
pub enum ConfigError {
EmptyHost,
EmptyPort,
MissingUserName,
MissingPassWord,
WrongPassWord,
}

impl fmt::Display for ConfigError {
Expand All @@ -310,6 +313,9 @@ impl fmt::Display for ConfigError {
match self {
Self::EmptyHost => f.write_str("no available host name found"),
Self::EmptyPort => f.write_str("no available host port found"),
Self::MissingUserName => f.write_str("username is missing"),
Self::MissingPassWord => f.write_str("password is missing"),
Self::WrongPassWord => f.write_str("password is wrong"),
}
}
}
Expand All @@ -318,29 +324,6 @@ impl error::Error for ConfigError {}

from_impl!(ConfigError);

/// error happens when library user failed to provide valid authentication info to database server.
#[derive(Debug)]
pub enum AuthenticationError {
MissingUserName,
MissingPassWord,
WrongPassWord,
}

impl fmt::Display for AuthenticationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::MissingUserName => f.write_str("username is missing")?,
Self::MissingPassWord => f.write_str("password is missing")?,
Self::WrongPassWord => f.write_str("password is wrong")?,
}
f.write_str(" for authentication")
}
}

impl error::Error for AuthenticationError {}

from_impl!(AuthenticationError);

#[non_exhaustive]
#[derive(Debug)]
pub enum SystemError {
Expand Down
4 changes: 1 addition & 3 deletions postgres/src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ use core::ops::{Deref, DerefMut, Range};
use postgres_protocol::message::{backend, frontend};
use xitca_io::bytes::BytesMut;

use crate::ExecuteBlocking;

use super::{
column::Column,
driver::codec::{self, encode::Encode, Response},
error::{Completed, Error},
execute::Execute,
execute::{Execute, ExecuteBlocking},
iter::AsyncLendingIterator,
query::Query,
row::Row,
Expand Down
12 changes: 6 additions & 6 deletions postgres/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use xitca_io::{bytes::BytesMut, io::AsyncIo};
use super::{
config::{Config, SslMode, SslNegotiation},
driver::generic::GenericDriver,
error::{AuthenticationError, Error},
error::{ConfigError, Error},
};

/// Properties required of a session.
Expand Down Expand Up @@ -163,17 +163,17 @@ where
match drv.recv().await? {
backend::Message::AuthenticationOk => return Ok(()),
backend::Message::AuthenticationCleartextPassword => {
let pass = cfg.get_password().ok_or(AuthenticationError::MissingPassWord)?;
let pass = cfg.get_password().ok_or(ConfigError::MissingPassWord)?;
send_pass(drv, pass, buf).await?;
}
backend::Message::AuthenticationMd5Password(body) => {
let pass = cfg.get_password().ok_or(AuthenticationError::MissingPassWord)?;
let user = cfg.get_user().ok_or(AuthenticationError::MissingUserName)?.as_bytes();
let pass = cfg.get_password().ok_or(ConfigError::MissingPassWord)?;
let user = cfg.get_user().ok_or(ConfigError::MissingUserName)?.as_bytes();
let pass = authentication::md5_hash(user, pass, body.salt());
send_pass(drv, pass, buf).await?;
}
backend::Message::AuthenticationSasl(body) => {
let pass = cfg.get_password().ok_or(AuthenticationError::MissingPassWord)?;
let pass = cfg.get_password().ok_or(ConfigError::MissingPassWord)?;

let mut is_scram = false;
let mut is_scram_plus = false;
Expand Down Expand Up @@ -227,7 +227,7 @@ where
_ => return Err(Error::todo()),
}
}
backend::Message::ErrorResponse(_) => return Err(Error::from(AuthenticationError::WrongPassWord)),
backend::Message::ErrorResponse(_) => return Err(Error::from(ConfigError::WrongPassWord)),
_ => {}
}
}
Expand Down

0 comments on commit 033e2f9

Please sign in to comment.