rustack_cloudfront_model/
error.rs1use thiserror::Error;
8
9#[derive(Debug, Error)]
13pub enum CloudFrontError {
14 #[error("{code}: {message}")]
16 NoSuchResource {
17 code: &'static str,
19 message: String,
21 },
22
23 #[error("InvalidArgument: {0}")]
25 InvalidArgument(String),
26
27 #[error("MissingArgument: {0}")]
29 MissingArgument(String),
30
31 #[error("ResourceInUse: {0}")]
33 ResourceInUse(String),
34
35 #[error("DistributionNotDisabled: {0}")]
38 DistributionNotDisabled(String),
39
40 #[error("PreconditionFailed: {0}")]
42 PreconditionFailed(String),
43
44 #[error("InvalidIfMatchVersion: {0}")]
46 InvalidIfMatchVersion(String),
47
48 #[error("InvalidArgument: {0}")]
50 MalformedInput(String),
51
52 #[error("{code}: {message}")]
54 AlreadyExists {
55 code: &'static str,
57 message: String,
59 },
60
61 #[error("NotImplemented: {0}")]
63 NotImplemented(String),
64
65 #[error("AccessDenied: {0}")]
67 AccessDenied(String),
68
69 #[error("InternalServerError: {0}")]
71 Internal(String),
72}
73
74impl CloudFrontError {
75 #[must_use]
77 pub fn code(&self) -> &'static str {
78 match self {
79 Self::NoSuchResource { code, .. } | Self::AlreadyExists { code, .. } => code,
80 Self::InvalidArgument(_) | Self::MalformedInput(_) => "InvalidArgument",
81 Self::MissingArgument(_) => "MissingArgument",
82 Self::ResourceInUse(_) => "ResourceInUse",
83 Self::DistributionNotDisabled(_) => "DistributionNotDisabled",
84 Self::PreconditionFailed(_) => "PreconditionFailed",
85 Self::InvalidIfMatchVersion(_) => "InvalidIfMatchVersion",
86 Self::NotImplemented(_) => "NotImplemented",
87 Self::AccessDenied(_) => "AccessDenied",
88 Self::Internal(_) => "InternalServerError",
89 }
90 }
91
92 #[must_use]
94 pub fn http_status(&self) -> u16 {
95 match self {
96 Self::NoSuchResource { .. } => 404,
97 Self::InvalidArgument(_)
98 | Self::MalformedInput(_)
99 | Self::MissingArgument(_)
100 | Self::InvalidIfMatchVersion(_) => 400,
101 Self::ResourceInUse(_)
102 | Self::AlreadyExists { .. }
103 | Self::DistributionNotDisabled(_) => 409,
104 Self::PreconditionFailed(_) => 412,
105 Self::NotImplemented(_) => 501,
106 Self::AccessDenied(_) => 403,
107 Self::Internal(_) => 500,
108 }
109 }
110
111 #[must_use]
113 pub fn message(&self) -> String {
114 match self {
115 Self::NoSuchResource { message, .. } | Self::AlreadyExists { message, .. } => {
116 message.clone()
117 }
118 Self::InvalidArgument(m)
119 | Self::MissingArgument(m)
120 | Self::ResourceInUse(m)
121 | Self::DistributionNotDisabled(m)
122 | Self::PreconditionFailed(m)
123 | Self::InvalidIfMatchVersion(m)
124 | Self::MalformedInput(m)
125 | Self::NotImplemented(m)
126 | Self::AccessDenied(m)
127 | Self::Internal(m) => m.clone(),
128 }
129 }
130}
131
132impl CloudFrontError {
134 #[must_use]
136 pub fn no_such_distribution(id: impl Into<String>) -> Self {
137 Self::NoSuchResource {
138 code: "NoSuchDistribution",
139 message: format!("The specified distribution does not exist: {}", id.into()),
140 }
141 }
142
143 #[must_use]
145 pub fn no_such_invalidation(id: impl Into<String>) -> Self {
146 Self::NoSuchResource {
147 code: "NoSuchInvalidation",
148 message: format!("The specified invalidation does not exist: {}", id.into()),
149 }
150 }
151
152 #[must_use]
154 pub fn no_such_origin_access_control(id: impl Into<String>) -> Self {
155 Self::NoSuchResource {
156 code: "NoSuchOriginAccessControl",
157 message: format!(
158 "The specified origin access control does not exist: {}",
159 id.into()
160 ),
161 }
162 }
163
164 #[must_use]
166 pub fn no_such_oai(id: impl Into<String>) -> Self {
167 Self::NoSuchResource {
168 code: "NoSuchCloudFrontOriginAccessIdentity",
169 message: format!(
170 "The specified origin access identity does not exist: {}",
171 id.into()
172 ),
173 }
174 }
175
176 #[must_use]
178 pub fn no_such_cache_policy(id: impl Into<String>) -> Self {
179 Self::NoSuchResource {
180 code: "NoSuchCachePolicy",
181 message: format!("The specified cache policy does not exist: {}", id.into()),
182 }
183 }
184
185 #[must_use]
187 pub fn no_such_origin_request_policy(id: impl Into<String>) -> Self {
188 Self::NoSuchResource {
189 code: "NoSuchOriginRequestPolicy",
190 message: format!(
191 "The specified origin request policy does not exist: {}",
192 id.into()
193 ),
194 }
195 }
196
197 #[must_use]
199 pub fn no_such_response_headers_policy(id: impl Into<String>) -> Self {
200 Self::NoSuchResource {
201 code: "NoSuchResponseHeadersPolicy",
202 message: format!(
203 "The specified response headers policy does not exist: {}",
204 id.into()
205 ),
206 }
207 }
208
209 #[must_use]
211 pub fn no_such_public_key(id: impl Into<String>) -> Self {
212 Self::NoSuchResource {
213 code: "NoSuchPublicKey",
214 message: format!("The specified public key does not exist: {}", id.into()),
215 }
216 }
217
218 #[must_use]
220 pub fn no_such_resource(kind: &'static str, id: impl Into<String>) -> Self {
221 Self::NoSuchResource {
222 code: "NoSuchResource",
223 message: format!("The specified {kind} does not exist: {}", id.into()),
224 }
225 }
226}