Skip to content

Commit

Permalink
handle S3 "KeyTooLongError" as PathNotFoundError in storage
Browse files Browse the repository at this point in the history
  • Loading branch information
syphar committed Jan 17, 2024
1 parent e3e1502 commit 3b11e0b
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions src/storage/s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::{Context as _, Error};
use aws_config::BehaviorVersion;
use aws_sdk_s3::{
config::{retry::RetryConfig, Region},
error::SdkError,
error::{ProvideErrorMetadata, SdkError},
operation::{get_object::GetObjectError, head_object::HeadObjectError},
types::{Delete, ObjectIdentifier, Tag, Tagging},
Client,
Expand All @@ -21,6 +21,19 @@ use tracing::{error, warn};
const PUBLIC_ACCESS_TAG: &str = "static-cloudfront-access";
const PUBLIC_ACCESS_VALUE: &str = "allow";

fn err_is_not_found<E>(err: &SdkError<E>) -> bool
where
E: ProvideErrorMetadata,
{
match err {
SdkError::ServiceError(err) => {
err.raw().status().as_u16() == http::StatusCode::NOT_FOUND.as_u16()
}
e if e.code() == Some("KeyTooLongError") => true,
_ => false,
}
}

pub(super) struct S3Backend {
client: Client,
bucket: String,
Expand Down Expand Up @@ -78,11 +91,11 @@ impl S3Backend {
{
Ok(_) => Ok(true),
Err(SdkError::ServiceError(err))
if (matches!(err.err(), HeadObjectError::NotFound(_))
|| err.raw().status().as_u16() == http::StatusCode::NOT_FOUND.as_u16()) =>
if matches!(err.err(), HeadObjectError::NotFound(_)) =>
{
Ok(false)
}
Err(err) if err_is_not_found(&err) => Ok(false),
Err(other) => Err(other.into()),
}
}
Expand All @@ -101,13 +114,7 @@ impl S3Backend {
.iter()
.filter(|tag| tag.key() == PUBLIC_ACCESS_TAG)
.any(|tag| tag.value() == PUBLIC_ACCESS_VALUE)),
Err(SdkError::ServiceError(err)) => {
if err.raw().status().as_u16() == http::StatusCode::NOT_FOUND.as_u16() {
Err(super::PathNotFoundError.into())
} else {
Err(err.into_err().into())
}
}
Err(err) if err_is_not_found(&err) => Err(super::PathNotFoundError.into()),
Err(other) => Err(other.into()),
}
}
Expand Down Expand Up @@ -139,13 +146,7 @@ impl S3Backend {
.await
{
Ok(_) => Ok(()),
Err(SdkError::ServiceError(err)) => {
if err.raw().status().as_u16() == http::StatusCode::NOT_FOUND.as_u16() {
Err(super::PathNotFoundError.into())
} else {
Err(err.into_err().into())
}
}
Err(err) if err_is_not_found(&err) => Err(super::PathNotFoundError.into()),
Err(other) => Err(other.into()),
}
}
Expand All @@ -163,16 +164,16 @@ impl S3Backend {
.key(path)
.set_range(range.map(|r| format!("bytes={}-{}", r.start(), r.end())))
.send()
.await
.map_err(|err| match err {
SdkError::ServiceError(err)
if (matches!(err.err(), GetObjectError::NoSuchKey(_))
|| err.raw().status().as_u16() == http::StatusCode::NOT_FOUND.as_u16()) =>
if matches!(err.err(), GetObjectError::NoSuchKey(_)) =>
{
super::PathNotFoundError.into()
}
err if err_is_not_found(&err) => super::PathNotFoundError.into(),
err => Error::from(err),
})
.await?;
})?;

let mut content = crate::utils::sized_buffer::SizedBuffer::new(max_size);
content.reserve(
Expand Down

0 comments on commit 3b11e0b

Please sign in to comment.