Skip to content

Commit

Permalink
Revamp errors in aws-inlineable
Browse files Browse the repository at this point in the history
  • Loading branch information
jdisanti committed Oct 31, 2022
1 parent bfa2b78 commit afa6186
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions aws/rust-runtime/aws-inlineable/src/presigning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,40 @@ pub mod config {
}
}

/// `PresigningConfig` build errors.
#[non_exhaustive]
#[derive(Debug)]
pub enum Error {
enum ErrorKind {
/// Presigned requests cannot be valid for longer than one week.
ExpiresInDurationTooLong,

/// The `PresigningConfig` builder requires a value for `expires_in`.
ExpiresInRequired,
}

/// `PresigningConfig` build errors.
#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
}

impl std::error::Error for Error {}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::ExpiresInDurationTooLong => {
match self.kind {
ErrorKind::ExpiresInDurationTooLong => {
write!(f, "`expires_in` must be no longer than one week")
}
Error::ExpiresInRequired => write!(f, "`expires_in` is required"),
ErrorKind::ExpiresInRequired => write!(f, "`expires_in` is required"),
}
}
}

impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Self {
Self { kind }
}
}

/// Builder used to create `PresigningConfig`.
#[non_exhaustive]
#[derive(Default, Debug)]
Expand Down Expand Up @@ -134,9 +144,9 @@ pub mod config {
/// Builds the `PresigningConfig`. This will error if `expires_in` is not
/// given, or if it's longer than one week.
pub fn build(self) -> Result<PresigningConfig, Error> {
let expires_in = self.expires_in.ok_or(Error::ExpiresInRequired)?;
let expires_in = self.expires_in.ok_or(ErrorKind::ExpiresInRequired)?;
if expires_in > ONE_WEEK {
return Err(Error::ExpiresInDurationTooLong);
return Err(ErrorKind::ExpiresInDurationTooLong.into());
}
Ok(PresigningConfig {
start_time: self.start_time.unwrap_or_else(SystemTime::now),
Expand Down

0 comments on commit afa6186

Please sign in to comment.