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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
use http::StatusCode;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use std::convert::TryFrom;
use thiserror::Error;
use crate::kv::KVError;
#[derive(Error, Debug, FromPrimitive)]
pub(crate) enum Error {
#[error("unknown")]
Unknown = -1,
#[error("http: invalid method")]
HTTPInvalidMethod = 0,
#[error("http: invalid status code")]
HTTPInvalidStatusCode = 1,
#[error("http: invalid version")]
HTTPInvalidVersion = 2,
#[error("http: invalid header name")]
HTTPInvalidHeaderName = 3,
#[error("http: invalid header value")]
HTTPInvalidHeaderValue = 4,
#[error("http: invalid uri")]
HTTPInvalidUri = 5,
#[error("http: invalid uri parts")]
HTTPInvalidUriParts = 6,
#[error("http: body size exceeds limit")]
HTTPTooLargeBody = 7,
#[error("http: URI length exceeds limit")]
HTTPTooLargeURI = 8,
#[error("http: header name length exceeds limit")]
HTTPTooLargeHeaderName = 9,
#[error("http: header value length exceeds limit")]
HTTPTooLargeHeaderValue = 10,
#[error("Storage: Empty content ")]
StorageEmptyContent = 11,
#[error("Storage: file_name is required")]
StorageMissingFileName = 12,
#[error("Storage: bucket_id is deleted")]
StorageInvalidBucketID = 13,
#[error("Storage: bucket_id is required")]
StorageMissingBucketID = 14,
#[error("Storage: Internal Error")]
StorageInternalError = 15,
#[error("Storage: datastore: not found")]
StorageContentNotFound = 16,
#[error("Storage: Unauthorized")]
StorageUnAuthorized = 17,
#[error("Storage: Content does not exist or deleted")]
StorageContentDeleted = 18,
#[error("Storage: Resource limit exceeded")]
StorageResourceLimit = 21,
#[error("kv: key not found")]
KVNotFound = 19,
#[error("kv: unauthorised operation")]
KVUnauthorised = 20,
#[error("stream: stream not found")]
StreamNotFound = 22,
#[error("stream: stream channel is closed")]
StreamChannelClosed = 23,
#[error("stream: read attempted on write stream")]
ReadOnWriteStream = 24,
#[error("stream: write attempted on read stream")]
WriteOnReadStream = 25,
#[error("stream: stream is closed")]
StreamClosed = 26,
#[error("stream: stream chunk is too large")]
StreamChunkTooLarge = 27,
#[error("fetch: HTTP fetch response not found")]
HTTPFetchResponseNotFound = 28,
#[error("fetch: HTTP fetch request failed")]
HTTPFetchRequestFailed = 29,
#[error("Storage: Storage response not found")]
StorageResponseNotFound = 30,
#[error("Channel Closed")]
ChannelClosed = 31,
}
#[non_exhaustive]
#[derive(Error, Copy, Clone, Debug, PartialEq, Eq)]
pub enum HttpError {
#[error("unknown")]
Unknown,
#[error("http header error : {0}")]
Header(#[from] HeaderError),
#[error("http body too large error")]
HTTPBodyTooLarge,
#[error("http uri error : {0}")]
Uri(#[from] UriError),
#[error("invalid fetch http method")]
HTTPInvalidMethod,
#[error("invalid response status code")]
HTTPInvalidStatusCode,
#[error("invalid http version")]
HTTPInvalidVersion,
#[error("HTTP fetch response not found")]
HTTPFetchResponseNotFound,
#[error("HTTP fetch request failed")]
HTTPFetchRequestFailed,
#[error("Channel Closed")]
HTTPChannelClosed,
}
impl From<Error> for HttpError {
fn from(e: Error) -> Self {
match e {
Error::Unknown => HttpError::Unknown,
Error::HTTPInvalidMethod => HttpError::HTTPInvalidMethod,
Error::HTTPInvalidStatusCode => HttpError::HTTPInvalidStatusCode,
Error::HTTPInvalidVersion => HttpError::HTTPInvalidVersion,
Error::HTTPInvalidHeaderName => HttpError::Header(HeaderError::InvalidName),
Error::HTTPInvalidHeaderValue => HttpError::Header(HeaderError::InvalidValue),
Error::HTTPInvalidUri | Error::HTTPInvalidUriParts => {
HttpError::Uri(UriError::InvalidUri)
}
Error::HTTPTooLargeBody => HttpError::HTTPBodyTooLarge,
Error::HTTPTooLargeURI => HttpError::Uri(UriError::TooLarge),
Error::HTTPTooLargeHeaderName => HttpError::Header(HeaderError::TooLargeName),
Error::HTTPTooLargeHeaderValue => HttpError::Header(HeaderError::TooLargeValue),
Error::HTTPFetchResponseNotFound => HttpError::HTTPFetchResponseNotFound,
Error::HTTPFetchRequestFailed => HttpError::HTTPFetchRequestFailed,
Error::ChannelClosed => HttpError::HTTPChannelClosed,
_ => HttpError::Unknown,
}
}
}
#[non_exhaustive]
#[derive(Error, Debug)]
pub enum StorageError {
#[error("Empty content")]
EmptyContent,
#[error("Storage: Missing query param 'file_name'")]
MissingFileName,
#[error("bucket_id is deleted")]
DeletedBucketID,
#[error("Missing query param 'bucket_id'")]
MissingBucketID,
#[error("Internal Error")]
InternalError,
#[error("bucket/content: not found")]
ContentNotFound,
#[error("Storage: Unauthorized")]
UnAuthorized,
#[error("Missing Attributes")]
MissingAttributes,
#[error("Content does not exist or deleted")]
ContentDeleted,
#[error("Invalid Attributes")]
InvalidAttributes(#[from] serde_json::error::Error),
#[error("Resource limit exceeded")]
ResourceLimit,
#[error("Storage: Storage response not found")]
StorageResponseNotFound,
#[error("Storage: Storage channel closed")]
StorageChannelClosed,
}
impl From<Error> for StorageError {
fn from(e: Error) -> Self {
match e {
Error::StorageEmptyContent => StorageError::EmptyContent,
Error::StorageMissingFileName => StorageError::MissingFileName,
Error::StorageInvalidBucketID => StorageError::DeletedBucketID,
Error::StorageMissingBucketID => StorageError::MissingBucketID,
Error::StorageInternalError => StorageError::InternalError,
Error::StorageContentNotFound => StorageError::ContentNotFound,
Error::StorageContentDeleted => StorageError::ContentDeleted,
Error::StorageResourceLimit => StorageError::ResourceLimit,
Error::StorageUnAuthorized => StorageError::UnAuthorized,
Error::StorageResponseNotFound => StorageError::StorageResponseNotFound,
Error::ChannelClosed => StorageError::StorageChannelClosed,
_ => StorageError::InternalError,
}
}
}
impl StorageError {
pub fn to_http_status_code(&self) -> StatusCode {
match &self {
StorageError::EmptyContent
| StorageError::MissingFileName
| StorageError::MissingBucketID
| StorageError::MissingAttributes
| StorageError::InvalidAttributes(_) => StatusCode::BAD_REQUEST,
StorageError::ContentNotFound
| StorageError::ContentDeleted
| StorageError::StorageResponseNotFound => StatusCode::NOT_FOUND,
StorageError::UnAuthorized => StatusCode::FORBIDDEN,
StorageError::ResourceLimit => StatusCode::UNPROCESSABLE_ENTITY,
StorageError::DeletedBucketID
| StorageError::InternalError
| StorageError::StorageChannelClosed => StatusCode::INTERNAL_SERVER_ERROR,
}
}
}
#[non_exhaustive]
#[derive(Error, Debug, FromPrimitive, Copy, Clone, PartialEq, Eq)]
pub enum HeaderError {
#[error("invalid header name")]
InvalidName = Error::HTTPInvalidHeaderName as isize,
#[error("invalid header value")]
InvalidValue = Error::HTTPInvalidHeaderValue as isize,
#[error("header name length exceeds limit")]
TooLargeName = Error::HTTPTooLargeHeaderName as isize,
#[error("header value length exceeds limit")]
TooLargeValue = Error::HTTPTooLargeHeaderValue as isize,
}
impl From<HeaderError> for Error {
fn from(e: HeaderError) -> Self {
Error::from_i32(e as i32).unwrap()
}
}
#[derive(Error, Debug)]
#[error("HTTP body too large")]
pub struct HTTPBodyTooLargeError();
impl From<HTTPBodyTooLargeError> for HttpError {
fn from(_: HTTPBodyTooLargeError) -> Self {
return HttpError::HTTPBodyTooLarge;
}
}
#[non_exhaustive]
#[derive(Error, Copy, Clone, Debug, PartialEq, Eq)]
pub enum UriError {
#[error("uri exceeds limit")]
TooLarge,
#[error("uri value invalid")]
InvalidUri,
}
impl From<UriError> for Error {
fn from(e: UriError) -> Self {
match e {
UriError::TooLarge => Error::HTTPTooLargeURI,
UriError::InvalidUri => Error::HTTPInvalidUri,
}
}
}
impl TryFrom<i32> for UriError {
type Error = &'static str;
fn try_from(value: i32) -> Result<Self, Self::Error> {
if value == Error::HTTPTooLargeURI as i32 {
Ok(UriError::TooLarge)
} else {
Err("unknown url related error code")
}
}
}
impl From<Error> for KVError {
fn from(e: Error) -> KVError {
match e {
Error::KVNotFound => KVError::NotFound,
Error::KVUnauthorised => KVError::UnAuthorized,
_ => KVError::Unknown,
}
}
}
#[derive(Error, Debug)]
pub enum StreamError {
#[error("unknown error")]
Unknown,
#[error("stream not found")]
StreamNotFound,
#[error("stream channel is closed")]
StreamChannelClosed,
#[error("read attempted on write stream")]
ReadOnWriteStream,
#[error("write attempted on read stream")]
WriteOnReadStream,
#[error("stream is closed")]
StreamClosed,
#[error("stream chunk is too large")]
StreamChunkTooLarge,
}
impl From<Error> for StreamError {
fn from(e: Error) -> StreamError {
match e {
Error::StreamNotFound => StreamError::StreamNotFound,
Error::StreamChannelClosed => StreamError::StreamChannelClosed,
Error::ReadOnWriteStream => StreamError::ReadOnWriteStream,
Error::WriteOnReadStream => StreamError::WriteOnReadStream,
Error::StreamClosed => StreamError::StreamClosed,
Error::StreamChunkTooLarge => StreamError::StreamChunkTooLarge,
_ => StreamError::Unknown,
}
}
}