Skip to content

Commit 7dd7b70

Browse files
committed
cloudfront: Extract CloudFrontError enum
1 parent 8dc3a5b commit 7dd7b70

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

src/cloudfront.rs

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,40 @@
11
use aws_credential_types::Credentials;
22
use aws_sdk_cloudfront::config::retry::RetryConfig;
33
use aws_sdk_cloudfront::config::{BehaviorVersion, Region};
4+
use aws_sdk_cloudfront::error::{BuildError, SdkError};
5+
use aws_sdk_cloudfront::operation::create_invalidation::CreateInvalidationError;
46
use aws_sdk_cloudfront::types::{InvalidationBatch, Paths};
57
use aws_sdk_cloudfront::{Client, Config};
68
use tracing::{debug, instrument, warn};
79

10+
#[derive(Debug, thiserror::Error)]
11+
pub enum CloudFrontError {
12+
#[error(transparent)]
13+
BuildError(#[from] BuildError),
14+
#[error(transparent)]
15+
SdkError(Box<SdkError<CreateInvalidationError>>),
16+
}
17+
18+
impl From<SdkError<CreateInvalidationError>> for CloudFrontError {
19+
fn from(err: SdkError<CreateInvalidationError>) -> Self {
20+
CloudFrontError::SdkError(Box::new(err))
21+
}
22+
}
23+
24+
impl CloudFrontError {
25+
pub fn is_too_many_invalidations_error(&self) -> bool {
26+
let CloudFrontError::SdkError(sdk_error) = self else {
27+
return false;
28+
};
29+
30+
let Some(service_error) = sdk_error.as_service_error() else {
31+
return false;
32+
};
33+
34+
service_error.is_too_many_invalidations_in_progress()
35+
}
36+
}
37+
838
pub struct CloudFront {
939
client: Client,
1040
distribution_id: String,
@@ -36,13 +66,13 @@ impl CloudFront {
3666
/// Invalidate a file on CloudFront
3767
///
3868
/// `path` is the path to the file to invalidate, such as `config.json`, or `re/ge/regex`
39-
pub async fn invalidate(&self, path: &str) -> anyhow::Result<()> {
69+
pub async fn invalidate(&self, path: &str) -> Result<(), CloudFrontError> {
4070
self.invalidate_many(vec![path.to_string()]).await
4171
}
4272

4373
/// Invalidate multiple paths on Cloudfront.
4474
#[instrument(skip(self))]
45-
pub async fn invalidate_many(&self, mut paths: Vec<String>) -> anyhow::Result<()> {
75+
pub async fn invalidate_many(&self, mut paths: Vec<String>) -> Result<(), CloudFrontError> {
4676
let now = chrono::offset::Utc::now().timestamp_micros();
4777

4878
// We need to ensure that paths have a starting slash.
@@ -72,15 +102,11 @@ impl CloudFront {
72102

73103
debug!("Sending invalidation request");
74104

75-
match invalidation_request.send().await {
76-
Ok(_) => {
77-
debug!("Invalidation request successful");
78-
Ok(())
79-
}
80-
Err(error) => {
81-
warn!(?error, "Invalidation request failed");
82-
Err(error.into())
83-
}
84-
}
105+
Ok(invalidation_request
106+
.send()
107+
.await
108+
.map(|_| ()) // We don't care about the result, just that it worked
109+
.inspect(|_| debug!("Invalidation request successful"))
110+
.inspect_err(|error| warn!(?error, "Invalidation request failed"))?)
85111
}
86112
}

0 commit comments

Comments
 (0)