reduct-base 1.19.8

Base crate for ReductStore
Documentation
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright 2021-2026 ReductSoftware UG
// Licensed under the Apache License, Version 2.0

pub use int_enum::IntEnum;
use std::error::Error;
use std::fmt::{Debug, Display, Error as FmtError, Formatter};
use std::sync::PoisonError;
use std::time::SystemTimeError;
use url::ParseError;

#[cfg(feature = "io")]
use tokio::sync::mpsc::error::SendError;

/// HTTP status codes + client errors (negative).
#[repr(i16)]
#[derive(Debug, PartialEq, PartialOrd, Copy, Clone, IntEnum)]
pub enum ErrorCode {
    InvalidRequest = -6, // used for invalid requests
    Interrupt = -5,      // used for interrupting a long-running task or query
    UrlParseError = -4,
    ConnectionError = -3,
    Timeout = -2,
    Unknown = -1,

    Continue = 100,
    OK = 200,
    Created = 201,
    Accepted = 202,
    NoContent = 204,
    BadRequest = 400,
    Unauthorized = 401,
    Forbidden = 403,
    NotFound = 404,
    MethodNotAllowed = 405,
    NotAcceptable = 406,
    RequestTimeout = 408,
    Conflict = 409,
    Gone = 410,
    LengthRequired = 411,
    PreconditionFailed = 412,
    PayloadTooLarge = 413,
    URITooLong = 414,
    UnsupportedMediaType = 415,
    RangeNotSatisfiable = 416,
    ExpectationFailed = 417,
    ImATeapot = 418,
    MisdirectedRequest = 421,
    UnprocessableEntity = 422,
    Locked = 423,
    FailedDependency = 424,
    TooEarly = 425,
    UpgradeRequired = 426,
    PreconditionRequired = 428,
    TooManyRequests = 429,
    RequestHeaderFieldsTooLarge = 431,
    UnavailableForLegalReasons = 451,
    InternalServerError = 500,
    NotImplemented = 501,
    BadGateway = 502,
    ServiceUnavailable = 503,
    GatewayTimeout = 504,
    HTTPVersionNotSupported = 505,
    VariantAlsoNegotiates = 506,
    InsufficientStorage = 507,
    LoopDetected = 508,
    NotExtended = 510,
    NetworkAuthenticationRequired = 511,
}

/// An HTTP error, we use it for error handling.
#[derive(PartialEq, Debug, Clone)]
pub struct ReductError {
    /// The HTTP status code.
    pub status: ErrorCode,

    /// The human-readable message.
    pub message: String,
}

impl Display for ReductError {
    fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
        write!(f, "[{:?}] {}", self.status, self.message)
    }
}

impl Display for ErrorCode {
    fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
        write!(f, "{}", i16::from(*self))
    }
}

impl From<std::io::Error> for ReductError {
    fn from(err: std::io::Error) -> Self {
        let status = match err.kind() {
            std::io::ErrorKind::Unsupported => ErrorCode::MethodNotAllowed,
            _ => ErrorCode::InternalServerError,
        };

        ReductError {
            status,
            message: err.to_string(),
        }
    }
}

impl From<SystemTimeError> for ReductError {
    fn from(err: SystemTimeError) -> Self {
        // A system time error is an internal reductstore error
        ReductError {
            status: ErrorCode::InternalServerError,
            message: err.to_string(),
        }
    }
}

impl From<ParseError> for ReductError {
    fn from(err: ParseError) -> Self {
        // A parse error is an internal reductstore error
        ReductError {
            status: ErrorCode::UrlParseError,
            message: err.to_string(),
        }
    }
}

impl<T> From<PoisonError<T>> for ReductError {
    fn from(_: PoisonError<T>) -> Self {
        // A poison error is an internal reductstore error
        ReductError {
            status: ErrorCode::InternalServerError,
            message: "Poison error".to_string(),
        }
    }
}

impl From<Box<dyn std::any::Any + Send>> for ReductError {
    fn from(err: Box<dyn std::any::Any + Send>) -> Self {
        // A box error is an internal reductstore error
        ReductError {
            status: ErrorCode::InternalServerError,
            message: format!("{:?}", err),
        }
    }
}

#[cfg(feature = "io")]
impl<T> From<SendError<T>> for ReductError {
    fn from(err: SendError<T>) -> Self {
        // A send error is an internal reductstore error
        ReductError {
            status: ErrorCode::InternalServerError,
            message: err.to_string(),
        }
    }
}

impl Error for ReductError {
    fn description(&self) -> &str {
        &self.message
    }
}

impl ReductError {
    pub fn new(status: ErrorCode, message: &str) -> Self {
        ReductError {
            status,
            message: message.to_string(),
        }
    }

    pub fn status(&self) -> ErrorCode {
        self.status
    }

    pub fn message(&self) -> &str {
        &self.message
    }

    pub fn ok() -> ReductError {
        ReductError {
            status: ErrorCode::OK,
            message: "".to_string(),
        }
    }

    pub fn timeout(msg: &str) -> ReductError {
        ReductError {
            status: ErrorCode::Timeout,
            message: msg.to_string(),
        }
    }

    /// Create a no content error.
    pub fn no_content(msg: &str) -> ReductError {
        ReductError {
            status: ErrorCode::NoContent,
            message: msg.to_string(),
        }
    }

    /// Create a not found error.
    pub fn not_found(msg: &str) -> ReductError {
        ReductError {
            status: ErrorCode::NotFound,
            message: msg.to_string(),
        }
    }

    /// Create a conflict error.
    pub fn conflict(msg: &str) -> ReductError {
        ReductError {
            status: ErrorCode::Conflict,
            message: msg.to_string(),
        }
    }

    /// Create a bad request error.
    pub fn bad_request(msg: &str) -> ReductError {
        ReductError {
            status: ErrorCode::BadRequest,
            message: msg.to_string(),
        }
    }

    /// Create an unauthorized error.
    pub fn unauthorized(msg: &str) -> ReductError {
        ReductError {
            status: ErrorCode::Unauthorized,
            message: msg.to_string(),
        }
    }

    /// Create a forbidden error.
    pub fn forbidden(msg: &str) -> ReductError {
        ReductError {
            status: ErrorCode::Forbidden,
            message: msg.to_string(),
        }
    }

    /// Create an unprocessable entity error.
    pub fn unprocessable_entity(msg: &str) -> ReductError {
        ReductError {
            status: ErrorCode::UnprocessableEntity,
            message: msg.to_string(),
        }
    }

    /// Create a too early error.
    pub fn too_early(msg: &str) -> ReductError {
        ReductError {
            status: ErrorCode::TooEarly,
            message: msg.to_string(),
        }
    }

    /// Create a bad request error.
    pub fn internal_server_error(msg: &str) -> ReductError {
        ReductError {
            status: ErrorCode::InternalServerError,
            message: msg.to_string(),
        }
    }
}

// Macros for creating errors with a message.

#[macro_export]
macro_rules! timeout {
    ($msg:expr, $($arg:tt)*) => {
        ReductError::timeout(&format!($msg, $($arg)*))
    };
    ($msg:expr) => {
        ReductError::timeout($msg)
    };
}

#[macro_export]
macro_rules! no_content {
    ($msg:expr, $($arg:tt)*) => {
        ReductError::no_content(&format!($msg, $($arg)*))
    };
    ($msg:expr) => {
        ReductError::no_content($msg)
    };
}

#[macro_export]
macro_rules! bad_request {
    ($msg:expr, $($arg:tt)*) => {
        ReductError::bad_request(&format!($msg, $($arg)*))
    };
    ($msg:expr) => {
        ReductError::bad_request($msg)
    };
}

#[macro_export]
macro_rules! forbidden {
    ($msg:expr, $($arg:tt)*) => {
        ReductError::forbidden(&format!($msg, $($arg)*))
    };
    ($msg:expr) => {
        ReductError::forbidden($msg)
    };
}

#[macro_export]
macro_rules! unprocessable_entity {
    ($msg:expr, $($arg:tt)*) => {
        ReductError::unprocessable_entity(&format!($msg, $($arg)*))
    };
    ($msg:expr) => {
        ReductError::unprocessable_entity($msg)
    };
}

#[macro_export]
macro_rules! not_found {
    ($msg:expr, $($arg:tt)*) => {
        ReductError::not_found(&format!($msg, $($arg)*))
    };
    ($msg:expr) => {
        ReductError::not_found($msg)
    };
}
#[macro_export]
macro_rules! conflict {
    ($msg:expr, $($arg:tt)*) => {
        ReductError::conflict(&format!($msg, $($arg)*))
    };
    ($msg:expr) => {
        ReductError::conflict($msg)
    };
}

#[macro_export]
macro_rules! too_early {
    ($msg:expr, $($arg:tt)*) => {
        ReductError::too_early(&format!($msg, $($arg)*))
    };
    ($msg:expr) => {
        ReductError::too_early($msg)
    };
}

#[macro_export]
macro_rules! internal_server_error {
    ($msg:expr, $($arg:tt)*) => {
        ReductError::internal_server_error(&format!($msg, $($arg)*))
    };
    ($msg:expr) => {
        ReductError::internal_server_error($msg)
    };
}

#[macro_export]
macro_rules! unauthorized {
    ($msg:expr, $($arg:tt)*) => {
        ReductError::unauthorized(&format!($msg, $($arg)*))
    };
    ($msg:expr) => {
        ReductError::unauthorized($msg)
    };
}

#[macro_export]
macro_rules! service_unavailable {
    ($msg:expr, $($arg:tt)*) => {
        ReductError::new(ErrorCode::ServiceUnavailable, &format!($msg, $($arg)*))
    };
    ($msg:expr) => {
        ReductError::new(ErrorCode::ServiceUnavailable, $msg)
    };
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::{SystemTime, UNIX_EPOCH};

    #[test]
    fn creates_internal_server_error() {
        let error = ReductError::internal_server_error("Unexpected server error");
        assert_eq!(error.status, ErrorCode::InternalServerError);
        assert_eq!(error.message, "Unexpected server error");
    }

    #[test]
    fn converts_io_error_to_reduct_error() {
        let io_error = std::io::Error::new(std::io::ErrorKind::Other, "IO failure");
        let error: ReductError = io_error.into();
        assert_eq!(error.status, ErrorCode::InternalServerError);
        assert_eq!(error.message, "IO failure");
    }

    #[test]
    fn converts_system_time_error_to_reduct_error() {
        let system_time_error = UNIX_EPOCH.duration_since(SystemTime::now()).unwrap_err();
        let error: ReductError = system_time_error.into();
        assert_eq!(error.status, ErrorCode::InternalServerError);
        assert_eq!(error.message, "second time provided was later than self");
    }

    #[test]
    fn converts_url_parse_error_to_reduct_error() {
        let parse_error = ParseError::EmptyHost;
        let error: ReductError = parse_error.into();
        assert_eq!(error.status, ErrorCode::UrlParseError);
        assert_eq!(error.message, "empty host");
    }

    #[test]
    fn converts_poison_error_to_reduct_error() {
        let poison_error: PoisonError<()> = PoisonError::new(());
        let error: ReductError = poison_error.into();
        assert_eq!(error.status, ErrorCode::InternalServerError);
        assert_eq!(error.message, "Poison error");
    }

    #[cfg(feature = "io")]
    #[test]
    fn converts_send_error_to_reduct_error() {
        let send_error: SendError<()> = SendError(());
        let error: ReductError = send_error.into();
        assert_eq!(error.status, ErrorCode::InternalServerError);
        assert_eq!(error.message, "channel closed");
    }

    mod macros {
        use super::*;

        #[test]
        fn test_timeout_macro() {
            let error = timeout!("Timeout error: {}", 42);
            assert_eq!(error.status, ErrorCode::Timeout);
            assert_eq!(error.message, "Timeout error: 42");
        }

        #[test]
        fn test_no_content_macro() {
            let error = no_content!("No content error: {}", 42);
            assert_eq!(error.status, ErrorCode::NoContent);
            assert_eq!(error.message, "No content error: 42");
        }

        #[test]
        fn test_bad_request_macro() {
            let error = bad_request!("Bad request error: {}", 42);
            assert_eq!(error.status, ErrorCode::BadRequest);
            assert_eq!(error.message, "Bad request error: 42");
        }

        #[test]
        fn test_unprocessable_entity_macro() {
            let error = unprocessable_entity!("Unprocessable entity error: {}", 42);
            assert_eq!(error.status, ErrorCode::UnprocessableEntity);
            assert_eq!(error.message, "Unprocessable entity error: 42");
        }

        #[test]
        fn test_not_found_macro() {
            let error = not_found!("Not found error: {}", 42);
            assert_eq!(error.status, ErrorCode::NotFound);
            assert_eq!(error.message, "Not found error: 42");
        }

        #[test]
        fn test_conflict_macro() {
            let error = conflict!("Conflict error: {}", 42);
            assert_eq!(error.status, ErrorCode::Conflict);
            assert_eq!(error.message, "Conflict error: 42");
        }

        #[test]
        fn test_too_early_macro() {
            let error = too_early!("Too early error: {}", 42);
            assert_eq!(error.status, ErrorCode::TooEarly);
            assert_eq!(error.message, "Too early error: 42");
        }

        #[test]
        fn test_internal_server_error_macro() {
            let error = internal_server_error!("Internal server error: {}", 42);
            assert_eq!(error.status, ErrorCode::InternalServerError);
            assert_eq!(error.message, "Internal server error: 42");
        }
    }
}