Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revamp errors in aws-config and aws-types #1809

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions aws/rust-runtime/aws-config/external-types.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ allowed_external_types = [
"http::uri::Uri",
"tower_service::Service",

# TODO(https://github.com/awslabs/smithy-rs/issues/1193): Decide if `InvalidUri` should be exposed
"http::uri::InvalidUri",

# TODO(https://github.com/awslabs/smithy-rs/issues/1193): Decide if the following should be exposed
"hyper::client::connect::Connection",
"tokio::io::async_read::AsyncRead",
Expand Down
7 changes: 4 additions & 3 deletions aws/rust-runtime/aws-config/src/credential_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use crate::json_credentials::{json_parse_loop, InvalidJsonCredentials, RefreshableCredentials};
use aws_smithy_json::deserialize::Token;
use aws_smithy_types::error::opaque::OpaqueError;
use aws_types::credentials::{future, CredentialsError, ProvideCredentials};
use aws_types::{credentials, Credentials};
use std::fmt;
Expand Down Expand Up @@ -197,7 +198,7 @@ pub(crate) fn parse_credential_process_json_credentials(
version = Some(i32::try_from(*value).map_err(|err| {
InvalidJsonCredentials::InvalidField {
field: "Version",
err: err.into(),
source: OpaqueError::new(err),
}
})?);
}
Expand Down Expand Up @@ -227,7 +228,7 @@ pub(crate) fn parse_credential_process_json_credentials(
Some(version) => {
return Err(InvalidJsonCredentials::InvalidField {
field: "version",
err: format!("unknown version number: {}", version).into(),
source: format!("unknown version number: {}", version).into(),
})
}
}
Expand All @@ -241,7 +242,7 @@ pub(crate) fn parse_credential_process_json_credentials(
SystemTime::try_from(OffsetDateTime::parse(&expiration, &Rfc3339).map_err(|err| {
InvalidJsonCredentials::InvalidField {
field: "Expiration",
err: err.into(),
source: OpaqueError::new(err),
}
})?)
.map_err(|_| {
Expand Down
101 changes: 56 additions & 45 deletions aws/rust-runtime/aws-config/src/ecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ use std::net::IpAddr;

use aws_smithy_client::erase::boxclone::BoxCloneService;
use aws_smithy_http::endpoint::Endpoint;
use aws_smithy_types::error::opaque::OpaqueError;
use aws_types::credentials;
use aws_types::credentials::{future, CredentialsError, ProvideCredentials};
use http::uri::{InvalidUri, Scheme};
use http::uri::Scheme;
use http::{HeaderValue, Uri};
use tower::{Service, ServiceExt};

use crate::http_credential_provider::HttpCredentialProvider;
use crate::provider_config::ProviderConfig;
use aws_smithy_client::http_connector::ConnectorSettings;
use aws_types::os_shim_internal::Env;
use http::header::InvalidHeaderValue;
use std::time::Duration;
use tokio::sync::OnceCell;

Expand Down Expand Up @@ -100,7 +100,7 @@ impl EcsCredentialsProvider {
Some(auth) => Some(HeaderValue::from_str(&auth).map_err(|err| {
tracing::warn!(token = %auth, "invalid auth token");
CredentialsError::invalid_configuration(EcsConfigurationErr::InvalidAuthToken {
err,
source: OpaqueError::new(err),
value: auth,
})
})?),
Expand Down Expand Up @@ -152,7 +152,10 @@ impl Provider {
let mut dns = dns.or_else(tokio_dns);
validate_full_uri(&full_uri, dns.as_mut())
.await
.map_err(|err| EcsConfigurationErr::InvalidFullUri { err, uri: full_uri })
.map_err(|err| EcsConfigurationErr::InvalidFullUri {
source: OpaqueError::new(err),
uri: full_uri,
})
} else {
Err(EcsConfigurationErr::NotConfigured)
}
Expand Down Expand Up @@ -184,7 +187,7 @@ impl Provider {
Err(invalid_uri) => {
tracing::warn!(uri = ?invalid_uri, "invalid URI loaded from environment");
return Err(EcsConfigurationErr::InvalidRelativeUri {
err: invalid_uri,
source: OpaqueError::new(invalid_uri),
uri: relative_uri,
});
}
Expand All @@ -197,40 +200,28 @@ impl Provider {

#[derive(Debug)]
enum EcsConfigurationErr {
InvalidRelativeUri {
err: InvalidUri,
uri: String,
},
InvalidFullUri {
err: InvalidFullUriError,
uri: String,
},
InvalidAuthToken {
err: InvalidHeaderValue,
value: String,
},
InvalidRelativeUri { source: OpaqueError, uri: String },
InvalidFullUri { source: OpaqueError, uri: String },
InvalidAuthToken { source: OpaqueError, value: String },
NotConfigured,
}

impl Display for EcsConfigurationErr {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
EcsConfigurationErr::InvalidRelativeUri { err, uri } => write!(
f,
"invalid relative URI for ECS provider ({}): {}",
err, uri
),
EcsConfigurationErr::InvalidFullUri { err, uri } => {
write!(f, "invalid full URI for ECS provider ({}): {}", err, uri)
EcsConfigurationErr::InvalidRelativeUri { source: _, uri } => {
write!(f, "invalid relative URI ({uri}) for ECS provider")
}
EcsConfigurationErr::InvalidFullUri { source: _, uri } => {
write!(f, "invalid full URI ({uri}) for ECS provider")
}
EcsConfigurationErr::NotConfigured => write!(
f,
"No environment variables were set to configure ECS provider"
),
EcsConfigurationErr::InvalidAuthToken { err, value } => write!(
EcsConfigurationErr::InvalidAuthToken { source: _, value } => write!(
f,
"`{}` could not be used as a header value for the auth token. {}",
value, err
"`{value}` could not be used as a header value for the auth token"
),
}
}
Expand All @@ -239,9 +230,10 @@ impl Display for EcsConfigurationErr {
impl Error for EcsConfigurationErr {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match &self {
EcsConfigurationErr::InvalidRelativeUri { err, .. } => Some(err),
EcsConfigurationErr::InvalidFullUri { err, .. } => Some(err),
_ => None,
Self::InvalidRelativeUri { source, .. } => Some(source),
Self::InvalidFullUri { source, .. } => Some(source),
Self::InvalidAuthToken { source, .. } => Some(source),
Self::NotConfigured => None,
}
}
}
Expand Down Expand Up @@ -310,7 +302,10 @@ impl Builder {
pub enum InvalidFullUriError {
/// The provided URI could not be parsed as a URI
#[non_exhaustive]
InvalidUri(InvalidUri),
InvalidUri {
/// Cause of the error
source: OpaqueError,
},

/// No Dns service was provided
#[non_exhaustive]
Expand All @@ -325,35 +320,51 @@ pub enum InvalidFullUriError {
NotLoopback,

/// DNS lookup failed when attempting to resolve the host to an IP Address for validation.
DnsLookupFailed(io::Error),
#[non_exhaustive]
DnsLookupFailed {
/// Cause of the error
source: OpaqueError,
},
}

impl InvalidFullUriError {
fn invalid_uri(source: impl Into<Box<dyn Error + Send + Sync + 'static>>) -> Self {
Self::InvalidUri {
source: OpaqueError::new(source),
}
}

fn dns_lookup_failed(source: impl Into<Box<dyn Error + Send + Sync + 'static>>) -> Self {
Self::DnsLookupFailed {
source: OpaqueError::new(source),
}
}
}

impl Display for InvalidFullUriError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
InvalidFullUriError::InvalidUri(err) => write!(f, "URI was invalid: {}", err),
InvalidFullUriError::MissingHost => write!(f, "URI did not specify a host"),
InvalidFullUriError::NotLoopback => {
Self::InvalidUri { source: _ } => write!(f, "invalid URI"),
Self::MissingHost => write!(f, "URI did not specify a host"),
Self::NotLoopback => {
write!(f, "URI did not refer to the loopback interface")
}
InvalidFullUriError::DnsLookupFailed(err) => {
Self::DnsLookupFailed { source: _ } => {
write!(
f,
"failed to perform DNS lookup while validating URI: {}",
err
"failed to perform DNS lookup while validating URI",
)
}
InvalidFullUriError::NoDnsService => write!(f, "No DNS service was provided. Enable `rt-tokio` or provide a `dns` service to the builder.")
Self::NoDnsService => write!(f, "No DNS service was provided. Enable `rt-tokio` or provide a `dns` service to the builder.")
}
}
}

impl Error for InvalidFullUriError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
InvalidFullUriError::InvalidUri(err) => Some(err),
InvalidFullUriError::DnsLookupFailed(err) => Some(err),
_ => None,
Self::InvalidUri { source, .. } | Self::DnsLookupFailed { source, .. } => Some(source),
Self::NoDnsService | Self::MissingHost | Self::NotLoopback => None,
}
}
}
Expand All @@ -373,7 +384,7 @@ async fn validate_full_uri(
) -> Result<Uri, InvalidFullUriError> {
let uri = uri
.parse::<Uri>()
.map_err(InvalidFullUriError::InvalidUri)?;
.map_err(InvalidFullUriError::invalid_uri)?;
if uri.scheme() == Some(&Scheme::HTTPS) {
return Ok(uri);
}
Expand All @@ -383,10 +394,10 @@ async fn validate_full_uri(
Ok(addr) => addr.is_loopback(),
Err(_domain_name) => {
let dns = dns.ok_or(InvalidFullUriError::NoDnsService)?;
dns.ready().await.map_err(InvalidFullUriError::DnsLookupFailed)?
dns.ready().await.map_err(InvalidFullUriError::dns_lookup_failed)?
.call(host.to_owned())
.await
.map_err(InvalidFullUriError::DnsLookupFailed)?
.map_err(InvalidFullUriError::dns_lookup_failed)?
.iter()
.all(|addr| {
if !addr.is_loopback() {
Expand Down
Loading