use thiserror::Error;
#[derive(Debug, Error)]
pub enum CloudFrontError {
#[error("{code}: {message}")]
NoSuchResource {
code: &'static str,
message: String,
},
#[error("InvalidArgument: {0}")]
InvalidArgument(String),
#[error("MissingArgument: {0}")]
MissingArgument(String),
#[error("ResourceInUse: {0}")]
ResourceInUse(String),
#[error("DistributionNotDisabled: {0}")]
DistributionNotDisabled(String),
#[error("PreconditionFailed: {0}")]
PreconditionFailed(String),
#[error("InvalidIfMatchVersion: {0}")]
InvalidIfMatchVersion(String),
#[error("InvalidArgument: {0}")]
MalformedInput(String),
#[error("{code}: {message}")]
AlreadyExists {
code: &'static str,
message: String,
},
#[error("NotImplemented: {0}")]
NotImplemented(String),
#[error("AccessDenied: {0}")]
AccessDenied(String),
#[error("InternalServerError: {0}")]
Internal(String),
}
impl CloudFrontError {
#[must_use]
pub fn code(&self) -> &'static str {
match self {
Self::NoSuchResource { code, .. } | Self::AlreadyExists { code, .. } => code,
Self::InvalidArgument(_) | Self::MalformedInput(_) => "InvalidArgument",
Self::MissingArgument(_) => "MissingArgument",
Self::ResourceInUse(_) => "ResourceInUse",
Self::DistributionNotDisabled(_) => "DistributionNotDisabled",
Self::PreconditionFailed(_) => "PreconditionFailed",
Self::InvalidIfMatchVersion(_) => "InvalidIfMatchVersion",
Self::NotImplemented(_) => "NotImplemented",
Self::AccessDenied(_) => "AccessDenied",
Self::Internal(_) => "InternalServerError",
}
}
#[must_use]
pub fn http_status(&self) -> u16 {
match self {
Self::NoSuchResource { .. } => 404,
Self::InvalidArgument(_)
| Self::MalformedInput(_)
| Self::MissingArgument(_)
| Self::InvalidIfMatchVersion(_) => 400,
Self::ResourceInUse(_)
| Self::AlreadyExists { .. }
| Self::DistributionNotDisabled(_) => 409,
Self::PreconditionFailed(_) => 412,
Self::NotImplemented(_) => 501,
Self::AccessDenied(_) => 403,
Self::Internal(_) => 500,
}
}
#[must_use]
pub fn message(&self) -> String {
match self {
Self::NoSuchResource { message, .. } | Self::AlreadyExists { message, .. } => {
message.clone()
}
Self::InvalidArgument(m)
| Self::MissingArgument(m)
| Self::ResourceInUse(m)
| Self::DistributionNotDisabled(m)
| Self::PreconditionFailed(m)
| Self::InvalidIfMatchVersion(m)
| Self::MalformedInput(m)
| Self::NotImplemented(m)
| Self::AccessDenied(m)
| Self::Internal(m) => m.clone(),
}
}
}
impl CloudFrontError {
#[must_use]
pub fn no_such_distribution(id: impl Into<String>) -> Self {
Self::NoSuchResource {
code: "NoSuchDistribution",
message: format!("The specified distribution does not exist: {}", id.into()),
}
}
#[must_use]
pub fn no_such_invalidation(id: impl Into<String>) -> Self {
Self::NoSuchResource {
code: "NoSuchInvalidation",
message: format!("The specified invalidation does not exist: {}", id.into()),
}
}
#[must_use]
pub fn no_such_origin_access_control(id: impl Into<String>) -> Self {
Self::NoSuchResource {
code: "NoSuchOriginAccessControl",
message: format!(
"The specified origin access control does not exist: {}",
id.into()
),
}
}
#[must_use]
pub fn no_such_oai(id: impl Into<String>) -> Self {
Self::NoSuchResource {
code: "NoSuchCloudFrontOriginAccessIdentity",
message: format!(
"The specified origin access identity does not exist: {}",
id.into()
),
}
}
#[must_use]
pub fn no_such_cache_policy(id: impl Into<String>) -> Self {
Self::NoSuchResource {
code: "NoSuchCachePolicy",
message: format!("The specified cache policy does not exist: {}", id.into()),
}
}
#[must_use]
pub fn no_such_origin_request_policy(id: impl Into<String>) -> Self {
Self::NoSuchResource {
code: "NoSuchOriginRequestPolicy",
message: format!(
"The specified origin request policy does not exist: {}",
id.into()
),
}
}
#[must_use]
pub fn no_such_response_headers_policy(id: impl Into<String>) -> Self {
Self::NoSuchResource {
code: "NoSuchResponseHeadersPolicy",
message: format!(
"The specified response headers policy does not exist: {}",
id.into()
),
}
}
#[must_use]
pub fn no_such_public_key(id: impl Into<String>) -> Self {
Self::NoSuchResource {
code: "NoSuchPublicKey",
message: format!("The specified public key does not exist: {}", id.into()),
}
}
#[must_use]
pub fn no_such_resource(kind: &'static str, id: impl Into<String>) -> Self {
Self::NoSuchResource {
code: "NoSuchResource",
message: format!("The specified {kind} does not exist: {}", id.into()),
}
}
}