Skip to content

Commit

Permalink
state machines for better error management, small futures, and cleare…
Browse files Browse the repository at this point in the history
…r flows
  • Loading branch information
conradludgate committed Jan 19, 2024
1 parent c65ac37 commit b38eedc
Show file tree
Hide file tree
Showing 27 changed files with 1,414 additions and 754 deletions.
13 changes: 13 additions & 0 deletions libs/pq_proto/src/framed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ impl<S> Framed<S> {
write_buf: self.write_buf,
})
}

/// Return new Framed with stream type transformed by f. For dynamic dispatch.
pub fn map_stream_sync<S2, F>(self, f: F) -> Framed<S2>
where
F: FnOnce(S) -> S2,
{
let stream = f(self.stream);
Framed {
stream,
read_buf: self.read_buf,
write_buf: self.write_buf,
}
}
}

impl<S: AsyncRead + Unpin> Framed<S> {
Expand Down
29 changes: 16 additions & 13 deletions proxy/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use password_hack::PasswordHackPayload;
mod flow;
pub use flow::*;

use crate::{console, error::UserFacingError};
use crate::error::{ReportableError, UserFacingError};
use std::io;
use thiserror::Error;

Expand All @@ -23,15 +23,6 @@ pub type Result<T> = std::result::Result<T, AuthError>;
/// Common authentication error.
#[derive(Debug, Error)]
pub enum AuthErrorImpl {
#[error(transparent)]
Link(#[from] backend::LinkAuthError),

#[error(transparent)]
GetAuthInfo(#[from] console::errors::GetAuthInfoError),

#[error(transparent)]
WakeCompute(#[from] console::errors::WakeComputeError),

/// SASL protocol errors (includes [SCRAM](crate::scram)).
#[error(transparent)]
Sasl(#[from] crate::sasl::Error),
Expand Down Expand Up @@ -99,13 +90,25 @@ impl<E: Into<AuthErrorImpl>> From<E> for AuthError {
}
}

impl ReportableError for AuthError {
fn get_error_type(&self) -> crate::error::ErrorKind {
match self.0.as_ref() {
AuthErrorImpl::Sasl(s) => s.get_error_type(),
AuthErrorImpl::BadAuthMethod(_) => crate::error::ErrorKind::User,
AuthErrorImpl::MalformedPassword(_) => crate::error::ErrorKind::User,
AuthErrorImpl::MissingEndpointName => crate::error::ErrorKind::User,
AuthErrorImpl::AuthFailed(_) => crate::error::ErrorKind::User,
AuthErrorImpl::Io(_) => crate::error::ErrorKind::Disconnect,
AuthErrorImpl::IpAddressNotAllowed => crate::error::ErrorKind::User,
AuthErrorImpl::TooManyConnections => crate::error::ErrorKind::RateLimit,
}
}
}

impl UserFacingError for AuthError {
fn to_string_client(&self) -> String {
use AuthErrorImpl::*;
match self.0.as_ref() {
Link(e) => e.to_string_client(),
GetAuthInfo(e) => e.to_string_client(),
WakeCompute(e) => e.to_string_client(),
Sasl(e) => e.to_string_client(),
AuthFailed(_) => self.to_string(),
BadAuthMethod(_) => self.to_string(),
Expand Down
Loading

0 comments on commit b38eedc

Please sign in to comment.