-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy patherror.rs
More file actions
196 lines (170 loc) · 6.42 KB
/
Copy patherror.rs
File metadata and controls
196 lines (170 loc) · 6.42 KB
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
//! Discriminated, stable error codes returned to the engine. Mirrors
//! `database/src/error.rs`. The `code` field is the contract; the rest
//! is diagnostic.
use crate::backend::BackendError;
use serde::Serialize;
use thiserror::Error;
#[derive(Debug, Error, Serialize)]
#[serde(tag = "code")]
pub enum StorageError {
#[serde(rename = "CONFIG_ERROR")]
#[error("config error: {message}")]
ConfigError { message: String },
#[serde(rename = "UNKNOWN_BUCKET")]
#[error("unknown bucket {bucket}")]
UnknownBucket { bucket: String },
#[serde(rename = "OBJECT_NOT_FOUND")]
#[error("object not found: {bucket}/{key}")]
ObjectNotFound { bucket: String, key: String },
#[serde(rename = "BODY_TOO_LARGE")]
#[error("body exceeds 10 MiB cap; use presignUrl for large objects")]
BodyTooLarge { size: u64, cap: u64 },
#[serde(rename = "OBJECT_TOO_LARGE")]
#[error("object exceeds 10 MiB cap; use presignUrl for large objects")]
ObjectTooLarge { size: u64, cap: u64 },
#[serde(rename = "INVALID_BASE64")]
#[error("invalid base64 payload: {reason}")]
InvalidBase64 { reason: String },
#[serde(rename = "INVALID_PRESIGN_PARAMS")]
#[error("invalid presign params: {reason}")]
InvalidPresignParams { reason: String },
#[serde(rename = "PRESIGN_UNSUPPORTED")]
#[error("presign unsupported on this backend: {reason}")]
PresignUnsupported { reason: String },
#[serde(rename = "LOCAL_BACKEND_DOWN")]
#[error("local rustfs backend is down: {reason}")]
LocalBackendDown { reason: String },
#[serde(rename = "LOCAL_BACKEND_BIN_NOT_FOUND")]
#[error("rustfs binary not found: {reason}")]
LocalBackendBinNotFound { reason: String },
#[serde(rename = "LOCAL_BACKEND_BOOT_FAILED")]
#[error("rustfs boot failed: {reason}")]
LocalBackendBootFailed { reason: String },
#[serde(rename = "PROVIDER_ERROR")]
#[error("provider {provider} error: {message}")]
ProviderError {
provider: String,
#[serde(rename = "inner_code")]
inner_code: Option<String>,
message: String,
},
#[serde(rename = "PROVIDER_AUTH_FAILED")]
#[error("provider {provider} auth failed: {message}")]
ProviderAuthFailed { provider: String, message: String },
#[serde(rename = "CF_QUEUE_AUTH_FAILED")]
#[error("cloudflare queue auth failed: {message}")]
CfQueueAuthFailed { message: String },
#[serde(rename = "TRIGGER_DISPATCH_TIMEOUT")]
#[error("trigger handler exceeded {timeout_ms}ms")]
TriggerDispatchTimeout { timeout_ms: u64 },
}
impl StorageError {
/// Convert into the JSON string body that handlers return as their `Err`.
/// Mirrors `database`'s `err_to_str`.
pub fn to_wire_string(&self) -> String {
serde_json::to_string(self).unwrap_or_else(|_| {
format!(
"{{\"code\":\"PROVIDER_ERROR\",\"message\":{:?}}}",
self.to_string()
)
})
}
}
impl From<StorageError> for iii_sdk::errors::Error {
fn from(e: StorageError) -> Self {
iii_sdk::errors::Error::Handler(e.to_wire_string())
}
}
/// Map a `BackendError` (provider-agnostic) into a `StorageError` (wire-stable).
/// The `provider` and `bucket`/`key` come from the call site since `BackendError`
/// alone doesn't know which provider was invoked.
pub fn backend_error_to_storage(
err: BackendError,
provider: &str,
bucket: &str,
key: &str,
) -> StorageError {
match err {
BackendError::NotFound => StorageError::ObjectNotFound {
bucket: bucket.to_string(),
key: key.to_string(),
},
BackendError::AuthFailed(message) => StorageError::ProviderAuthFailed {
provider: provider.to_string(),
message,
},
BackendError::PresignUnsupported(reason) => StorageError::PresignUnsupported { reason },
BackendError::LocalBackendDown(reason) => StorageError::LocalBackendDown { reason },
BackendError::ObjectTooLarge { actual_size, cap } => StorageError::ObjectTooLarge {
size: actual_size,
cap,
},
BackendError::Provider {
inner_code,
message,
} => StorageError::ProviderError {
provider: provider.to_string(),
inner_code,
message,
},
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::Value;
#[test]
fn body_too_large_serializes_with_stable_code() {
let e = StorageError::BodyTooLarge {
size: 20_000_000,
cap: 10 * 1024 * 1024,
};
let v: Value = serde_json::to_value(&e).unwrap();
assert_eq!(v["code"], "BODY_TOO_LARGE");
assert_eq!(v["size"], 20_000_000);
}
#[test]
fn object_not_found_includes_bucket_and_key() {
let e = StorageError::ObjectNotFound {
bucket: "uploads".into(),
key: "u/1/profile.jpg".into(),
};
let v: Value = serde_json::to_value(&e).unwrap();
assert_eq!(v["code"], "OBJECT_NOT_FOUND");
assert_eq!(v["bucket"], "uploads");
assert_eq!(v["key"], "u/1/profile.jpg");
}
#[test]
fn provider_error_includes_inner_code() {
let e = StorageError::ProviderError {
provider: "s3".into(),
inner_code: Some("PermanentRedirect".into()),
message: "wrong region".into(),
};
let v: Value = serde_json::to_value(&e).unwrap();
assert_eq!(v["code"], "PROVIDER_ERROR");
assert_eq!(v["provider"], "s3");
assert_eq!(v["inner_code"], "PermanentRedirect");
}
#[test]
fn backend_not_found_maps_to_object_not_found() {
let s = backend_error_to_storage(BackendError::NotFound, "s3", "uploads", "k");
let v = serde_json::to_value(&s).unwrap();
assert_eq!(v["code"], "OBJECT_NOT_FOUND");
}
#[test]
fn backend_auth_failed_maps_to_provider_auth_failed() {
let s =
backend_error_to_storage(BackendError::AuthFailed("expired".into()), "s3", "u", "k");
let v = serde_json::to_value(&s).unwrap();
assert_eq!(v["code"], "PROVIDER_AUTH_FAILED");
assert_eq!(v["provider"], "s3");
}
#[test]
fn into_iii_error_carries_wire_body() {
let e = StorageError::UnknownBucket { bucket: "x".into() };
let iii_e: iii_sdk::errors::Error = e.into();
let body = format!("{iii_e:?}");
assert!(body.contains("UNKNOWN_BUCKET"));
}
}