1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use axum::http::StatusCode;
use serde::Serialize;
use serde_json::{Map, Value};
/// Represents an AWS API error response.
#[derive(Debug, Clone, Serialize)]
pub struct AwsError {
/// HTTP status code (e.g., 404, 400, 500)
#[serde(skip)]
pub status: StatusCode,
/// AWS error code (e.g., "NoSuchBucket", "ResourceNotFoundException")
pub code: String,
/// Human-readable error message
pub message: String,
/// Error type: "Sender" (client error) or "Receiver" (server error)
pub error_type: ErrorType,
/// Extra JSON fields merged into the serialized error body.
///
/// Some AWS exceptions carry structured data alongside the standard
/// `__type` / `message` envelope — for example, DynamoDB's
/// `TransactionCanceledException` includes a `CancellationReasons` array,
/// and `ConditionalCheckFailedException` may include the existing `Item`.
/// Use [`Self::with_extras`] or [`Self::with_extra`] to attach them.
///
/// Boxed to keep `AwsError` small enough to fit comfortably in a
/// `Result<_, AwsError>` (clippy's `result_large_err` threshold).
#[serde(skip_serializing_if = "Option::is_none")]
pub extras: Option<Box<Map<String, Value>>>,
}
#[derive(Debug, Clone, Serialize)]
pub enum ErrorType {
Sender,
Receiver,
}
impl AwsError {
pub fn not_found(code: impl Into<String>, message: impl Into<String>) -> Self {
Self {
status: StatusCode::NOT_FOUND,
code: code.into(),
message: message.into(),
error_type: ErrorType::Sender,
extras: None,
}
}
/// Service-level "resource not found" error returned with HTTP 400.
///
/// Many JSON-protocol services (DynamoDB, KMS, SecretsManager, Cognito, ...)
/// model `ResourceNotFoundException` and friends as client-side validation
/// errors and respond with `400 Bad Request` rather than `404 Not Found`.
/// Use this constructor for those cases; reserve [`Self::not_found`] for
/// REST-style 404s such as S3's `NoSuchBucket` / `NoSuchKey`.
pub fn service_not_found(code: impl Into<String>, message: impl Into<String>) -> Self {
Self {
status: StatusCode::BAD_REQUEST,
code: code.into(),
message: message.into(),
error_type: ErrorType::Sender,
extras: None,
}
}
pub fn bad_request(code: impl Into<String>, message: impl Into<String>) -> Self {
Self {
status: StatusCode::BAD_REQUEST,
code: code.into(),
message: message.into(),
error_type: ErrorType::Sender,
extras: None,
}
}
/// HTTP 416 Range Not Satisfiable — used by S3 when a `Range` header
/// requests bytes outside the object's size.
pub fn range_not_satisfiable(code: impl Into<String>, message: impl Into<String>) -> Self {
Self {
status: StatusCode::RANGE_NOT_SATISFIABLE,
code: code.into(),
message: message.into(),
error_type: ErrorType::Sender,
extras: None,
}
}
/// HTTP 412 Precondition Failed — used by S3 when an `If-Match` /
/// `If-Unmodified-Since` conditional request fails.
pub fn precondition_failed(code: impl Into<String>, message: impl Into<String>) -> Self {
Self {
status: StatusCode::PRECONDITION_FAILED,
code: code.into(),
message: message.into(),
error_type: ErrorType::Sender,
extras: None,
}
}
/// HTTP 429 Too Many Requests — used by services that throttle on
/// concurrency or rate. Lambda raises this with code
/// `TooManyRequestsException`; DynamoDB uses
/// `ProvisionedThroughputExceededException`.
pub fn too_many_requests(code: impl Into<String>, message: impl Into<String>) -> Self {
Self {
status: StatusCode::TOO_MANY_REQUESTS,
code: code.into(),
message: message.into(),
error_type: ErrorType::Sender,
extras: None,
}
}
pub fn conflict(code: impl Into<String>, message: impl Into<String>) -> Self {
Self {
status: StatusCode::CONFLICT,
code: code.into(),
message: message.into(),
error_type: ErrorType::Sender,
extras: None,
}
}
pub fn internal(message: impl Into<String>) -> Self {
Self {
status: StatusCode::INTERNAL_SERVER_ERROR,
code: "InternalServiceError".to_string(),
message: message.into(),
error_type: ErrorType::Receiver,
extras: None,
}
}
pub fn not_implemented(operation: &str) -> Self {
Self {
status: StatusCode::NOT_IMPLEMENTED,
code: "NotImplemented".to_string(),
message: format!("Operation '{operation}' is not yet implemented in AWSim"),
error_type: ErrorType::Receiver,
extras: None,
}
}
pub fn unknown_operation(operation: &str) -> Self {
Self {
status: StatusCode::BAD_REQUEST,
code: "UnknownOperationException".to_string(),
message: format!("Unknown operation: {operation}"),
error_type: ErrorType::Sender,
extras: None,
}
}
pub fn access_denied(message: impl Into<String>) -> Self {
Self {
status: StatusCode::FORBIDDEN,
code: "AccessDeniedException".to_string(),
message: message.into(),
error_type: ErrorType::Sender,
extras: None,
}
}
/// HTTP 403 with a service-specific error code (e.g. Cognito's
/// `NotAuthorizedException` or SNS's `AuthorizationError`).
pub fn forbidden(code: impl Into<String>, message: impl Into<String>) -> Self {
Self {
status: StatusCode::FORBIDDEN,
code: code.into(),
message: message.into(),
error_type: ErrorType::Sender,
extras: None,
}
}
pub fn access_denied_for(action: &str, principal_arn: &str, resource: &str) -> Self {
Self {
status: StatusCode::FORBIDDEN,
code: "AccessDenied".to_string(),
message: format!(
"User: {principal_arn} is not authorized to perform: {action} on resource: {resource}"
),
error_type: ErrorType::Sender,
extras: None,
}
}
pub fn validation(message: impl Into<String>) -> Self {
Self {
status: StatusCode::BAD_REQUEST,
code: "ValidationException".to_string(),
message: message.into(),
error_type: ErrorType::Sender,
extras: None,
}
}
/// Replace the extras map wholesale.
pub fn with_extras(mut self, extras: Map<String, Value>) -> Self {
self.extras = Some(Box::new(extras));
self
}
/// Insert a single extra field, allocating the map if needed.
pub fn with_extra(mut self, key: impl Into<String>, value: Value) -> Self {
self.extras
.get_or_insert_with(|| Box::new(Map::new()))
.insert(key.into(), value);
self
}
}
impl std::fmt::Display for AwsError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.code, self.message)
}
}
impl std::error::Error for AwsError {}