Unverified Commit 543cac37 authored by John DiSanti's avatar John DiSanti Committed by GitHub
Browse files

Revamp errors in AWS runtime crates (#1922)

Revamp errors in:
  * `aws-types`
  * `aws-endpoint`
  * `aws-http`
  * `aws-sig-auth`
  * `aws-inlineable`
parent 8dfe5a1f
Loading
Loading
Loading
Loading
+23 −6
Original line number Diff line number Diff line
@@ -13,11 +13,29 @@ use crate::imds::client::{ImdsError, LazyClient};
use crate::json_credentials::{parse_json_credentials, JsonCredentials, RefreshableCredentials};
use crate::provider_config::ProviderConfig;
use aws_smithy_client::SdkError;
use aws_smithy_types::error::display::DisplayErrorContext;
use aws_types::credentials::{future, CredentialsError, ProvideCredentials};
use aws_types::os_shim_internal::Env;
use aws_types::{credentials, Credentials};
use std::borrow::Cow;
use std::error::Error as StdError;
use std::fmt;

#[derive(Debug)]
struct ImdsCommunicationError {
    source: Box<dyn StdError + Send + Sync + 'static>,
}

impl fmt::Display for ImdsCommunicationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "could not communicate with IMDS")
    }
}

impl StdError for ImdsCommunicationError {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        Some(self.source.as_ref())
    }
}

/// IMDSv2 Credentials Provider
///
@@ -138,11 +156,10 @@ impl ImdsCredentialsProvider {
                );
                Err(CredentialsError::not_loaded("received 404 from IMDS"))
            }
            Err(ImdsError::FailedToLoadToken(ref err @ SdkError::DispatchFailure(_))) => {
                Err(CredentialsError::not_loaded(format!(
                    "could not communicate with IMDS: {}",
                    DisplayErrorContext(&err)
                )))
            Err(ImdsError::FailedToLoadToken(err @ SdkError::DispatchFailure(_))) => {
                Err(CredentialsError::not_loaded(ImdsCommunicationError {
                    source: err.into(),
                }))
            }
            Err(other) => Err(CredentialsError::provider_error(other)),
        }
+5 −5
Original line number Diff line number Diff line
@@ -82,12 +82,12 @@ impl CredentialsProviderChain {
                    tracing::debug!(provider = %name, "loaded credentials");
                    return Ok(credentials);
                }
                Err(CredentialsError::CredentialsNotLoaded { context, .. }) => {
                    tracing::debug!(provider = %name, context = %context, "provider in chain did not provide credentials");
                Err(err @ CredentialsError::CredentialsNotLoaded(_)) => {
                    tracing::debug!(provider = %name, context = %DisplayErrorContext(&err), "provider in chain did not provide credentials");
                }
                Err(e) => {
                    tracing::warn!(provider = %name, error = %DisplayErrorContext(&e), "provider failed to provide credentials");
                    return Err(e);
                Err(err) => {
                    tracing::warn!(provider = %name, error = %DisplayErrorContext(&err), "provider failed to provide credentials");
                    return Err(err);
                }
            }
        }
+2 −1
Original line number Diff line number Diff line
@@ -266,6 +266,7 @@ mod test {
    };
    use aws_sdk_sts::Region;
    use aws_smithy_async::rt::sleep::TokioSleep;
    use aws_smithy_types::error::display::DisplayErrorContext;
    use aws_types::credentials::CredentialsError;
    use aws_types::os_shim_internal::{Env, Fs};
    use std::collections::HashMap;
@@ -308,7 +309,7 @@ mod test {
            .await
            .expect_err("should fail, provider not loaded");
        assert!(
            format!("{}", err).contains("AWS_ROLE_ARN"),
            format!("{}", DisplayErrorContext(&err)).contains("AWS_ROLE_ARN"),
            "`{}` did not contain expected string",
            err
        );
+1 −1
Original line number Diff line number Diff line
@@ -2,6 +2,6 @@
  "name": "imds-default-chain",
  "docs": "IMDS isn't specifically configured but is loaded as part of the default chain. This has the exact same HTTP traffic as imds_no_iam_role, they are equivalent.",
  "result": {
    "ErrorContains": "The credential provider was not enabled"
    "ErrorContains": "the credential provider was not enabled"
  }
}
+1 −1
Original line number Diff line number Diff line
@@ -2,6 +2,6 @@
  "name": "imds-disabled",
  "docs": "when IMDS is disabled by an environment variable, it shouldn't be used as part of the default chain",
  "result": {
    "ErrorContains": "The credential provider was not enabled"
    "ErrorContains": "the credential provider was not enabled"
  }
}
Loading