use aws_credential_types::provider::error::CredentialsError;
use aws_sdk_bedrockruntime::config::http::HttpResponse;
use aws_sdk_bedrockruntime::error::{ProvideErrorMetadata, SdkError};
use aws_smithy_types::error::display::DisplayErrorContext;
use super::arn::mask_account_ids;
use crate::inference::error::BedrockError;
pub(crate) fn classify_send_error<E>(
operation: &str,
model: &str,
region: &str,
err: &SdkError<E, HttpResponse>,
) -> BedrockError
where
E: ProvideErrorMetadata + std::error::Error + 'static,
{
let message = mask_account_ids(&format!(
"{operation} call failed (model={model}, region={region}): {}",
DisplayErrorContext(err)
))
.into_owned();
if chain_has_credentials_error(err) {
return BedrockError::MissingCredential(message);
}
match err {
SdkError::TimeoutError(_) => BedrockError::Timeout(message),
SdkError::DispatchFailure(d) if d.is_timeout() => BedrockError::Timeout(message),
SdkError::DispatchFailure(_) => BedrockError::Transport(message),
SdkError::ServiceError(ctx) => BedrockError::Api {
status: ctx.raw().status().as_u16(),
code: ctx.err().code().map(str::to_owned),
message,
},
_ => BedrockError::Other(message),
}
}
fn chain_has_credentials_error(err: &(dyn std::error::Error + 'static)) -> bool {
let mut current = Some(err);
while let Some(e) = current {
if e.is::<CredentialsError>() {
return true;
}
current = e.source();
}
false
}